Just as the scope of a variable depends on where and how it is declared, the
scope of a watch depends on the manner of its definition. Specifying the scope
of a watch determines the context in which its behavior is observed. If you know
where in your program the observation of a variable is significant, you can make
more efficient use of your debugging time by specifying the appropriate Watch
You are already familiar with the issue of scope as it pertains to data variables.
In a block-structured programming language such as Visual Basic, the visibility
of a variable depends on where it is declared. A variable may exist at any one
of the following three levels of scope:
A variable declared in the context of a particular procedure is visible only
in the context of that procedure. It is said to be local in its scope, which means
that it is invisible to all other procedures in the program. Consider the following
subprocedures, for instance:
Dim iCount as Integer, sName as String
‘ pretend that something useful happens later in the
Dim iCount as Integer
‘ pretend that something else useful happens here, too
The variables declared in these routines are all local in scope. The variable
sName declared in MyProcedure can be accessed only
by the code in MyProcedure. It is not visible to
YourProcedure, nor is it visible to any other routine in the program. The
two iCount variables are each local to the procedures
in which they are declared. They are permitted to have the same name because each
is unique in the context of the procedure in which it is declared.
A variable may also have module scope if it is declared in the General Declaration
section of its code module using the keyword Private. In this case, the variable
is visible to all procedures contained in the module, but it is invisible to all
other procedures in the program.
Finally, a variable is global in scope if it is declared in the General Declaration
section of its code module using the keyword Public. Such a variable is visible
to all procedures contained throughout the program.
If you already understand how variables may have their scope defined at these
different levels, you will find the scope of a watch fairly easy to understand.
Just as a variable’s scope may be at the local, module, or global level,
the scope of a watch may be defined at these levels too.