After you have entered an expression to watch, you need to tell VB about the
context in which the watch should be active. Remember that the watch expression
must be in the scope of the currently executing code for VB to tell you its value.
If you specify a context in which the expression isn’t valid, you will see
the Expression not defined in context message. Again, that message doesn’t
necessarily mean that you have entered a bad expression. It does pay, however,
to check the entry just in case.
If you don’t have a form or module open when you create your watch, the
context options for your watch defaults to all procedures and all modules.
If you accept the default context options, the expression is evaluated constantly
throughout the entire project. Such a broad setting may sometimes make sense (as
for a global variable, for instance), but you will make VB work harder if you
monitor Watch expressions in every possible context. The narrower the scope, the
faster you see the results. Bear in mind that the goal of a watch is to locate
a problem in your code, such as a failure to modify a variable or assigning it
a bad value.
If you have some idea of where the problem is, it makes sense to limit the
watch to a more appropriate context. When you set a watch, VB will change the
module context setting for you to default to the form or module you are currently
viewing, as shown in Figure 18.4.
FIGURE 18.4 You can limit the scope of your Watch expression to a single module.
The default procedure selection is also context sensitive. If the cursor is
in a particular procedure, it will be used as the default scope, as shown in Figure
FIGURE 18.5 You can also limit the scope of a Watch expression to a particular
If you have an expression selected in a code window, the value of the Watch
expression defaults to it too. If you want to watch a variable, you don’t
need to highlight its entire name—the word in which the cursor appears will
be used by default. Of course, you aren’t locked into the defaults. You
can type a different expression for the watch if the default isn’t what
you want. If you need to select a different scope, you can select from a list
of the modules and procedures in the current project, as shown in Figure 18.6.
FIGURE 18.6 Use the combo boxes to change the module or procedure scope of
a Watch expression.