Using the Watches Window
The Watches window is similar to the Locals window in that it enables you to see at a glance one or more simple or user-defined types.
The Watches window, shown in Figure 3.5, looks similar to the Locals window, but you add only those variables you want to examine in the Watches window. The variables you add can be local to the subroutine or function, but they also can be variables outside of the subroutine or function.
Figure 3.5 The Watches window enables you to add variables, as well _as expressions, to examine.
The Watches window has four columns. The Expression column contains a variable or an evaluation using an operator, and the Value column contains the value of the expression. The Type column contains the data type of the expression. This is the data type of the variable or a Boolean, if the expression uses an operator. The Context column is the module name and procedure name connected by a dot operator (represented by a period). For this example, I use the default module name, Module1, and I name the subroutine CalculateCircumference. Therefore, the Context column of the Watches window contains Module1.CalculateCircumference.
Adding a Watch Item
The most important difference between the Watches and Locals windows is that Watches enables you to define expressions that appear in the window. I will use Figure 3.6 as a point of reference to describe the benefits of the Watches window. Variables to watch are added in the Add Watch dialog shown in Figure 3.6.
Figure 3.6 Adding a variable watch.
You add a watch by selecting the Debug, Add Watch menu item. Watches are most useful when the code is running, but you can prepare them before you run your code. Referring to the Add Watch dialog in Figure 3.6, you add a watch by placing an expression in the Expression text box. An expression can be just a variable name, or you can use an operator expression, such as Circumference > 300. The context can be set manually; by default, the Watches window uses the current context. Context refers to where the debugger is currently stopped, indicated by the yellow-highlighted line of code. The Procedure text box specifies the function or subroutine. The Module text box specifies the current module, by default; however, keep mind that your program can have more than one module. The key part of the Add Watch dialog is the Watch Type area (refer to Figure 3.6).
The Watch Type area enables you to instruct the editor under what condition, relative to the watch expression, the debugger should stop. In Figure 3.6, I selected the Break When Value Is True watch type. Therefore, when Circumference is greater than 300, represented by the expression Circumference > 300, the debugger stops.
If you are using the F8 key to step through the code, your program executes only one line at a time. However, if you want to stop the program only if the expression is True, you can press the F5 key.
Editing Watches
The Edit Watch dialog is the same as the Add Watch dialog. The only difference is that when you open the Edit Watch dialog, it contains the currently selected expression in the Watches window. To edit a watch, perform the following steps:
Click the watch value you want to edit in the Watch window.
Click the Debug menu.
Click Edit Watch.
The Edit Watch dialog should open with the expression you selected in the Watches window. The Edit Watch dialog is similar to the add Watch dialog; the caption of the Edit Watch window is "Edit Watch" and the Edit Watch window contains an additional Delete button, to allow you to remove the watch.
Using Quick Watches
You can use a quick watch when you want to take a quick peek at the value of a variable or an expression already defined in your code. For example, you could perform a quick watch on the expression Radius ^ 2 to evaluate only that part of the expression. Follow these steps to complete a quick watch:
While your program is in debug mode and paused, highlight the expression you want to evaluate.
Click the Debug menu.
Click the Quick Watch menu item.
If you perform the preceding steps over Radius ^ 2, you will see a Quick Watch dialog similar to the one shown in Figure 3.7.
Figure 3.7 Use the Quick Watch dialog to evaluate variables and subexpressions quickly.
As you may recall from Listing 3.3, Radius was assigned the value 10. The square of 10 is, of course, 100. The watch and quick watch are useful tools for testing your programming and learning to use different kinds of data.