Monitoring Data with Watch Expressions

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.

Adding a Watch Expression

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

  1. From the Debug menu, choose Add Watch.

  2. The current expression (if any) in the Code Editor will appear in the Expression box on the Add Watch dialog box. If this isn't the expression you want to watch, enter the expression to evaluate in the Expression box.

  3. If necessary, set the scope of the variables to watch.

    If you select the Procedure or Module option under Context, select a procedure, form, or module name from the appropriate list box.

  4. If necessary, select an option button in the Watch Type group to determine how you want Visual Basic to respond to the watch expression.

  5. Choose OK.

Note   You can also add an expression by dragging and dropping from the Code Editor to the Watch window.

Editing or Deleting a Watch Expression

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

  1. In the Watch window, double click the watch expression you want to edit.

    -or-

    Select the watch expression you want to edit and choose Edit Watch from the Debug menu.

  2. The Edit Watch dialog box is displayed and is identical to the Add Watch dialog box except for the title bar and the addition of a Delete button.

  3. Make any changes to the expression, the scope for evaluating variables, or the watch type.

  4. Choose OK.

To delete a watch expression

In the Watch window, select the watch expression you want to delete.

Identifying Watch Types

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

Using Quick Watch

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

  1. Select a watch expression in the Code window.

  2. Click the Quick Watch button on the Debug toolbar. (To display the Debug toolbar, right-click on the Visual Basic toolbar and select the Debug option.)

    -or-

    Press SHIFT+F9.

    -or-

    From the Debug menu, choose Quick Watch.

  3. If you want to add a watch expression based on the expression in the Quick Watch dialog box, choose the Add button.