While you are busy stepping through your code, you can watch the values of
your Watch expressions in the Watch window. For instance, consider a loop such
Dim i as Integer, j as Integer, k as Integer
Dim astrAlphabet(0 to 25) as String
j = LBound(astrAlphabet)
k = UBound(astrAlphabet)
For i = j to k
astrAlphabet (i) = Chr$( i + 65)
This loop populates a 26-element array with the letters of the alphabet. If
you set a breakpoint and create a watch on the astrAlphabet array, you can watch
the values of each element of the array as they are assigned values in the loop.
However, you won’t see the array build a string such as "ABCDEFGHIJKLMNOPQRSTUVWXYZ".
In fact, if you only watch the single line containing the name of the array, you
won’t see much of anything useful except for the range of the array index
values (in this case, 0 to 25). That’s because an array is a data structure
that contains more than one value, and each line in the Watch window can only
display an individual value.
Don’t worry. You don’t need to set a separate watch for each element
of the array. When dealing with a data structure such as an array, the Watch window
will display the name of the array with a boxed plus sign next to it, as shown
in Figure 18.9. It works in the same way that Windows Explorer displays a disk
drive’s directory structure in a tree control. Just as Windows Explorer
uses plus signs to indicate that nested subdirectories remain to be displayed,
the VB Watch window uses plus signs to indicate that more data elements are nested
within the selected structure.
FIGURE 18.9. When a watch is set on an array, you will see a boxed plus sign
next to it.
If you click the plus sign, the structure unfolds to display each individual
data element, as shown in Figure 18.10. The plus sign changes to a minus sign,
which you can click on to hide the elements contained in the structure again.
FIGURE 18.10. Click on the plus sign to monitor the values of the elements
contained in the array.
Watching User-Defined Types
The same nesting principle applies to user-defined types. If you create a type
to store an employee’s name, ID number, and Social Security number, it might
look like this:
strName as String
iIDNumber as Integer
lSocSecNum as Long
If you create a variable of type tEmployee, and
then set a watch on that variable, the Watch window will display the name of your
variable and a plus sign. As with the array, you need to click on the plus sign
to unfold the strName, iIDNumber, and
lSocSecNum elements of the data structure.
It is also possible to have multiple levels of nested structures in the Watch
window. If you were to create an array of tEmployee variables
called atMyEmployees, for example, you would need to
click on the plus sign associated with the atMyEmployees array
to display the tEmployee elements. To see the values
contained for each tEmployee, you would in turn have
to click on the plus sign associated with each element in the array.