One of VB’s debugging windows can save you from endlessly embedding
statements in code to examine the value of variables: the Locals
window, which displays the current value of variables within the scope of the
currently executing procedure.
Because what it displays depends on the current scope, the contents of the
Locals window changes whenever a different procedure executes. To display the
Locals window, pull down the View menu and choose Locals Window. Figure 18.22
shows the Locals window.
Calling it a "Locals" window is somewhat misleading, because it
isn’t limited to viewing only those variables that are local in scope. You
will see all the local variables, of course, but form-level variables are also
visible in the Locals window. (Remember that you will only see the form-level
variables for the current form.) For module-level variables, you have to use the
other methods covered in this chapter (for example, the Watch window and
Debug.Print). The Locals window can’t display module-level variables.
FIGURE 18.22 The Locals window.
Here is an example. Module_1 contains nothing but
a global variable, declared like this:
Public g_Test As Integer
In Form1, a form-level variable is declared in the
General Declarations section:
Public f_Test As Long
Here is the rest of the Form1 code:
Private Sub Form_Initialize()
Dim i as integer
i = 1
g_Test = 10
f_Test = 1000
Private Sub Form_Click()
Dim k as integer
k = 5
Values are assigned to the form-level variable and the module-level variable
in the form’s Initialize event. When the form
is clicked, the program enters Break mode. This is necessary because the contents
of the Locals window depend on a specific local scope. After the program enters
Break mode, you can use the Locals window to examine the status of the variables.
When the program enters Break mode, the Locals window looks like Figure 18.23.
FIGURE 18.23 The local variable and ME are available in the Locals window.
Because program execution was broken in the form’s
Click event, the value of k is available because
it is local to that event. Notice what isn’t available: You can’t
see the value of the i variable from the Initialized
event because it isn’t local to the current procedure. You also can’t
see the form-level variable f_Test or the module-level
variable g_Test. You do see something called ME, however,
which has a boxed plus sign immediately to its left.
You may recall that ME is VB shorthand for the form in which code is currently
executing. If you click on the plus sign, the ME object will unfold, displaying
all the current form’s properties in alphabetic order. Look toward the bottom
of the window displayed here and you will see that f_Test
is displayed, but g_Test is not. A form-level
variable is essentially a property of a form, and so it is available here. The
module-level variable, however, is not. What happened to k?
It is still there, but when you clicked on ME to display its contents, it scrolled
to the very bottom of the window.
At the top of the Locals window, notice that the name of the currently executing
procedure is identified. To the right of the procedure name, the button with the
ellipsis can be used to display the Call Stack window. The Locals window also
enables you to do one more trick: You can use it to change the value of a property
or a variable. If you click on the value displayed for an item, you can type a
new value to replace it. The change won’t take effect until you press Enter.