![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6!
Context And Watch Expressions I have mentioned the term context a couple of times in relation to watch expressions. Context is closely related to the concept of variable scope that I covered in Chapter 5. To jog your memory, scope refers to the parts of a Visual Basic project in which a particular variable is visible or accessible. Likewise, the context of a watch expression determines the parts of the project where the expression is evaluated. Any watch expressions context has three parts:
Generally, you should leave the context settings at their default values for any watch expression. Extending an expressions context beyond the default setting will slow program execution and should be done only when you have a specific reason to do so. When the program is in break mode and a watch expression displays <Out of context> in the Watches window, it simply means that the current break location is outside that expressions context. This is rarely a problem; in parts of the program where a variable or property is out of context, it should not be contributing to any bugs. Quick WatchesIf you want to take a quick look at the value of a variable or expression, you can use a quick watch. While your program is in break mode, position the editing cursor on the variable or property name you are interested in or highlight the expression you want to evaluate. Then press Shift+F9 or select Quick Watch from the Debug menu. The Quick Watch dialog box is displayed, as shown in Figure 26.4. The parts of this dialog box are as follows:
While the Quick Watch dialog box is displayed, click on the Add button to add the expression to the Watches window. Click on Cancel to close the dialog box.
Using The Immediate WindowThe Immediate window is automatically displayed in the Visual Basic environment when you run a program. It does not display anything at first, and in fact, it will remain blank if you do not use it. It is called the Immediate window, because any code you type into it is executed immediately. You can use the Immediate window whether or not a program is executing. To display it when no program is executing, select Immediate Window from the View menu or press Ctrl+G. When a program is not executing, the primary use for the Immediate window is to evaluate Basic expressions. If you type a line of Basic code in the Immediate window (or copy and paste it from the Code Editing window) and then press Enter, the code is executed immediately. Typically, you will use the Print statement to evaluate the value of expressions. Thus, if you enter print cos(.5) and press Enter, Visual Basic will display 0.877582561890373 in the Immediate window. The Immediate window is much more useful in break mode, when program execution is paused. Then, you can view and change the value of any variables and properties that are available within the current context. For example, if execution is paused within a procedure that declares a variable named Total, then executing Print Total in the Immediate window will display the current value of the variable. Executing Total = 10 will change the value of Total to 10. You are probably thinking you can accomplish these same tasksviewing and changing a variable or propertys valuefrom the Watches window, and you are correct. The Immediate window, however, provides additional flexibility, permitting you to perform these tasks without cluttering the Watches window with too many expressions. Controlling Execution During Debugging The Debug menu has several commands that give you some control over the path of program execution. Two of these commands are used in break mode and are related to the next statement to be executed when execution resumes (by selecting Continue):
Why would you want to change the next statement? When the program is paused in break mode, you can use this command to move the next statement back, so that one or more statements immediately before the break location will be executed again. You can also move it ahead, so that one or more statements immediately following the break location will be skipped. Combined with changing variable values (using the Watches and Immediate windows, as described earlier), this technique can be powerful in helping you locate the program locations where bugs occur. The Run to Cursor command on the Debug menu can be useful in a similar manner. Selecting this command while in break mode causes execution to continue up to the line of code where the cursor is located. Execution will stop at any breakpoints encountered along the way, however. Single Stepping Your Program When debugging a program, youll find it useful at times to step through the program one line at a time. No, you do not have to set a breakpoint on every single line of code. Instead, use the Step commands from the Debug menu. These commands are listed in Table 26.2. You can see that Step Into and Step Over have the same effect if the next statement is not a procedure call. Distributing Your ProgramIf the Visual Basic program you are writing is for your own use only, simply make an EXE file and place the executable in the appropriate folder on your hard disk. For most programs, however, you will need some way to distribute them to other usersfrom a few colleagues in your department to millions of customers worldwide. Fortunately, Visual Basic provides tools to simplify this task.
|
![]() |
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |