If you’re waiting to see where a watch value changes, the process of
manually executing each individual line of your code can get to be awfully tedious,
particularly if you’re executing a lengthy loop. Waiting for the 26 letters
of the alphabet to be assigned is no big deal, but what if you’re processing
hundreds or thousands of records? Rather than forcing you to manually execute
the code until the change occurs (or until the F8 key on your keyboards wears
out), VB gives you a couple of more convenient options.
Breaking on True
One of your options is to set a watch that causes your program to enter Break
mode only when the value of the watch is True. This kind of watch spares you the
bother of manually stepping through each individual line of code, taking you directly
to Break mode when the program gets to a point that is really interesting.
Here’s a simple procedure to illustrate the point:
AN ILLUSTRATIVE PROGRAM
Private Sub Form_Click()
Dim iCounter As Integer, iTotal As Integer
Dim fIncrementTotal As Boolean
Print "From 1 to 100, the even multiples of 7 are:"
For iCounter = 1 To 100
‘ set flag if value is an even multiple of 7
fIncrementTotal = Not CBool(iCounter Mod 7)
If fIncrementTotal Then
‘ sum even multiples of 7
iTotal = iTotal + iCounter
Print "The sum of these values is " & iTotal
If you set a simple watch on the iTotal variable, you would have to set a breakpoint
and then manually execute every subsequent line to observe changes in iTotal’s
value. If you instead set a watch that causes the program to enter Break mode
only when fIncrementTotal is true, however, you will save yourself some drudgery.
To set this kind of watch, just select the appropriate option in the Watch dialog
box, as shown in Figure 18.11.
FIGURE 18.11 Select Break When Value Is True to enter Break mode automatically
when a value is True.
When you hit the first breakpoint, you can inspect the values of the program
to make sure your calculations are correct. Instead of stepping through each individual
line, you can select Continue and the program will run normally until the Watch
expression evaluates to True again, at which point you are back in Break mode.
Breaking on Change
Another way to evaluate this procedure is to set a watch so that the program
enters Break mode only when the value of a Watch expression changes, as shown
in Figure 18.12. In this case, you could set this type of watch on the iTotal
variable. The program would only enter Break mode when iTotal changes. This accomplishes
the same thing as the preceding example, of course, because iTotal is incremented
only when fIncrementTotal is True.
FIGURE 18.12 Select Break When Value Changes to enter Break mode automatically
when a value changes.
When you are working with different kinds of watches simultaneously, VB makes
it easy to see what kind of watches are in play by displaying a different icon
for each type in the Watch window, as shown in Figure 18.13. An eyeglasses icon
denotes a simple Watch expression.
FIGURE 18.13 Each type of watch is associated with its own icon in the Watches
Break When Value Is True and Break When Value Changes both use an open palm
to indicate that these watches use VB’s internal breakpoint traffic cop
to force your program into Break mode. The Break on Change icon also includes
a small triangle, which you may also recognize as the Greek letter "delta,"
that is often used to indicate changes in a value. Break on True displays a small
blue rectangle in addition to the open palm, which is perhaps meant to call the
expression "true blue" to mind.
If you find that setting a watch that enters Break mode is more convenient
than using the simple Watch expression you have already entered, remember that
you can always change a watch from one type to another via the Debug menu’s
Edit Watch selection. A rightclick of the mouse in the Watch window also displays
this option on the pop-up menu.