Visual Basic offers three Watch types. Each Watch type determines a slightly
different behavior for the Watch expression when you run your code at design time.
Remember that you choose the Watch type when you add or edit a Watch expression.
The following three sections discuss these types of Watch expression.
Watch Type Watch Expression
The default Watch type is Watch expression. This Watch type will just list
the expression’s name and value (when in scope) in the Watch window. Whenever
you want to know an expression’s current value, you can access the Watch
window with View, Watch Window.
Watch Type Break When Value Changes
If you select the Break When Value Changes Watch type in the Add Watch dialog
box, Visual Basic will enter Break mode whenever the value of the expression changes
during execution. A Watch type of Break When Value Changes automates the process
of monitoring the variable. When you set up a watch of this type, the Watch window
(with a row listing the variable’s current value) will come up automatically
every time the variable’s value changes.
Watch Type Break When Value Is True
This Watch type causes Visual Basic to enter Break mode when the value of the
expression becomes True during execution. This type is handy when you’re
uncertain about the exact behavior of the expression and there is complicated
code involved.
Unless the variable you’re monitoring is of Boolean type, you typically
won’t stipulate a variable’s name by itself in the Expression field
of the Add Watch dialog box. Instead, you will put some type of comparison expression
involving the variable, because what you’re trying to do here is refine
how often VB will break to the Watch window.
For example, your code includes a variable that is changed inside a loop. You
are not, however, interested in having your code break every time the variable’s
value changes. You need only to know when it takes on a certain range of values,
say values greater than 100. In that case, you might specify the following expression:
variablename > 100
Now, instead of being interrupted on every pass through the loop, VB notifies
you only when the expression evaluates to True. Of course, observing the expression’s
current value (which will be either True or False) in the Watch window doesn’t
give you much information about the variable’s precise value. Therefore,
you will typically use this type of watch in tandem with a simple nonbreaking
watch—that is, a Watch type of Watch expression.
As an example, imagine a loop with this line:
intAccumulator = intAccumulator + SquareInt(intCounter)
You want to make sure that intAccumulator never approaches a value that could
max out the carrying capacity of the Integer type, that is 32KB. To test this,
you could create two Watch expressions:
-
The first Watch expression’s type would be Watch expression and the expression
would be just the name of the variable, intAccumulator.
-
The second Watch expression would be of the type Break When Expression Is True,
and the expression would be something like intAccumulator >=15000. (If you
waited for exactly 32KB, it would be too late!)
Therefore, when you run your application from the VB design environment with
these two watches set, you will see nothing unless intAccumulator reaches 30,000
or more. If that ever happens, VB will go into Break mode, the Watch window will
come up, and you will see the exact value on a line in the Watch window.