in Figure 6-4. (See the upcoming “Using the Locals Window” and “Using the
Watches Window” sections for other techniques.) To see this view, simply
hover the mouse over any variable, even an object.
Using
the Immediate window
to your advantage
The Immediate window is a valuable debugging tool. You can display the
Immediate window by clicking the Immediate Window button on the Debug
toolbar (refer to Figure 6-3). In Chapter 1, I show how you can actually create
a mini-program by using the Immediate window. It
can perform simple assign-
ments, and you can use it to determine the value of a variable.
The Immediate window can act as an output screen. The most common way
to use the Immediate window as an output is the
Debug.Print
method.
Here in
Listing 6-3 is some code showing how to use this valuable debugging
method. (You can find the source code for this example on the Dummies.com
site at
http://www.dummies.com/go/vbafd5e
.)
Listing 6-3
Using the Debug Object
Public Sub UseDebug()
‘ The variable that receives the input.
Dim InNumber As Byte
‘ Ask the user for some input.
InNumber = InputBox(“Type a number between 1 and “ + _
“10.”, “Numeric Input”, “1”)
‘ Print the value of InNumber to the Immediate window.
Debug.Print “InNumber = “ + CStr(InNumber)
‘ Stop program execution if InNumber is not in the
‘ correct range.
Figure 6-4:
Rely on
data tips
whenever
possible to
see the
value of a
variable.
150
Part II: Learning the Ropes
11_046500 ch06.qxp 12/5/06 5:35 PM Page 150
Debug.Assert (InNumber >= 1) And (InNumber <= 10)
‘ Display the result.
MsgBox “The Number You Typed: “ + CStr(InNumber), _
vbOKOnly Or vbInformation, _
“Successful Input”
End Sub
Notice
how this sample uses the
Debug.Print
and the
Debug.Assert
methods in combination. The
Debug.Print
method outputs the current
values to the Immediate window, and the
Debug.Assert
method
checks for
a specific input range. When the range is incorrect, the program breaks, and
you can see the errant value in the Immediate window.
Using the Locals Window
The Locals window shows all the variables that
the current code segment can
see. You can see variables defined within the current
Sub
or
Function
as
well as global variables. You display the Locals window by clicking the Locals
Window button on the toolbar. Figure 6-5 shows a typical example of the
Locals window.
The Locals window displays three kinds of information:
variable name, value,
and data type. You can see two global variables at the top of Figure 6-5.
Immediately below the global variables is
FreeSpace
, a local
Double
. The
current value is the amount of space on the C drive of my system.
Do'stlaringiz bilan baham: