Visual Basic Concepts
As you debug your application, a calculation may not produce the result you want or problems might occur when a certain variable or property assumes a particular value or range of values. Many debugging problems aren't immediately traceable to a single statement, so you may need to observe the behavior of a variable or expression throughout a procedure.
Visual Basic automatically monitors watch expressions — expressions that you define — for you. When the application enters break mode, these watch expressions appear in the Watch window, where you can observe their values.
You can also direct watch expressions to put the application into break mode whenever the expression's value changes or equals a specified value. For example, instead of stepping through perhaps tens or hundreds of loops one statement at a time, you can use a watch expression to put the application in break mode when a loop counter reaches a specific value. Or you may want the application to enter break mode each time a flag in a procedure changes value.
You can add a watch expression at design time or in break mode. You use the Add Watch dialog box (shown in Figure 13.12) to add watch expressions.
Figure 13.12 The Add Watch dialog box
The following table describes the Add Watch dialog box.
Component | Description |
Expression box | Where you enter the expression that the watch expression evaluates. The expression is a variable, a property, a function call, or any other valid expression. |
Context option group | Sets the scope of variables watched in the expression. Use if you have variables of the same name with different scope. You can also restrict the scope of variables in watch expressions to a specific procedure or to a specific form or module, or you can have it apply to the entire application by selecting All Procedures and All Modules. Visual Basic can evaluate a variable in a narrow context more quickly. |
Watch Type option group | Sets how Visual Basic responds to the watch expression. Visual Basic can watch the expression and display its value in the Watch window when the application enters break mode. Or you can have the application enter break mode automatically when the expression evaluates to a true (nonzero) statement or each time the value of the expression changes. |
To add a watch expression
If you select the Procedure or Module option under Context, select a procedure, form, or module name from the appropriate list box.
Note You can also add an expression by dragging and dropping from the Code Editor to the Watch window.
The Edit Watch dialog box, shown in Figure 13.13, lists all the current watch expressions. You can edit and delete any watch listed in the Watch window.
Figure 13.13 The Edit Watch dialog box
To edit a watch expression
-or-
Select the watch expression you want to edit and choose Edit Watch from the Debug menu.
To delete a watch expression
In the Watch window, select the watch expression you want to delete.
At the left edge of each watch expression in the Watch window is an icon identifying the watch type of that expression. Figure 13.14 defines the icon for each of the three watch types.
Figure 13.14 Watch type icons
While in break mode, you can check the value of a property, variable, or expression for which you have not defined a watch expression. To check such expressions, use the Quick Watch dialog box, shown in Figure 13.15.
Figure 13.15 The Quick Watch dialog box
The Quick Watch dialog box shows the value of the expression you select from the Code window. To continue watching this expression, click the Add button; the Watch window, with relevant information from the Quick Watch dialog box already entered, is displayed. If Visual Basic cannot evaluate the value of the current expression, the Add button is disabled.
To display the Quick Watch dialog box
-or-
Press SHIFT+F9.
-or-
From the Debug menu, choose Quick Watch.