A watch enables you to observe the value of an expression while your code executes.
To create a Watch expression, open a project and pull down the Debug menu to choose
Add Watch, as shown in Figure 18.1.
After you click on the Add Watch option, you will see the dialog box shown
in Figure 18.2.
When you create a watch, you can specify any valid VB expression. The expression
may range in complexity from the name of a single variable to a calculation involving
a series of nested function calls.
As long as it is a legal VB expression, you can monitor its value while your
code executes.
If you make a mistake when you type the expression, however, VB won’t
tell you about it. There is no Watch expression equivalent of the VB syntax checker
to tell you that you have made a mistake after you enter a line. Consequently,
it is up to you to pay close attention to ensure that you type what you intended.
If you type nonsense that VB can’t evaluate at runtime, the Watch window
will cheerfully display it for you in the Watch window without complaint, as shown
in Figure 18.3.

FIGURE 18.1 Use the Debug menu to create a Watch expression.

FIGURE 18.2 The Add Watch dialog box.

FIGURE 18.3 If your expression can’t be evaluated, the Watches window
says so.
The closest thing you will get to an error message is the text in the Value
column of the Watch window that says Expression not defined in context, but you
will see that message under normal circumstances anyway. That is because the mechanism
that enables you to set watches isn’t omniscient. The same rules that apply
to standard VB code also govern the evaluation of a Watch expression. That means
that the expression being watched must be in the scope of the currently executing
code for the Watch window to report its value.
That is why you don’t get an error message in the Watch window even if
you try to set a watch for nonsense. The Watch expression is evaluated only during
runtime when VB is in Debug mode. You can, however, enter Watch expressions when
VB is in Design mode.
You don’t need to type the expression you want to watch. You can double-click
on a variable name to select it, and then drag it to the Watch window. Likewise,
if you highlight an expression, the expression can be dragged to the Watch window.
Whatever you drag into the Watch window is automatically used to create a new
Watch expression.
If you need to edit an existing watch to fix a mistake, select it in the Watch
window, and then pull down the Debug menu to choose Edit Watch. You can also right-click
the Watch window to add, edit, or delete a watch. Another way to delete a watch
is to select it in the Watch window and press the Delete key.
As usual, VB gives you a lot of different ways to manage the Watch window itself.
You can let the Watch window float or drag it to a location on your screen to
dock in a fixed position. Grab its title bar with the mouse to drag it from one
place to another. Doubleclicking the title bar toggles its status between docked
and floating.