Useful Versus .NET Debugging Tools
Breakpoints are great, but they're not enough. You need to be able to view
and modify data, find out how the execution path got you to the current
location, and what's going on in the environment. To help you out, Visual
Studio .NET provides many useful tools you can use while debugging. The
following subsections show some of the tools you may want to investigate.
Watching Variables Using Your Mouse
If you want to investigate the value of a simple variable while debugging
in Break mode, you could use the Immediate window or one of several other
windows. The simplest technique, however, is to simply hover the mouse
pointer over the variable. Visual Studio .NET will display a tool tip
containing the name of the variable and the value contained within the
variable. Figure 9.11 shows the tool tip while in Break mode.
Figure 9.11. Hover your cursor over a variable in Break mode to see the
value of that variable.
Watch Window
You can use the Watch window to view the value of variables and expressions
as your code is executing. This can be useful if you want to be able to
single-step through code, and for each line of code, investigate the value
of one or more variables or expressions. Use the Debug, Windows, Watch menu
to display the window shown in Figure 9.12. You can add new variables or
expressions just by typing within the Name column. You can also modify the
values of your variables (but not expressions, of course) by typing within
the Value column.
Figure 9.12. The Watch window can display the value of variables or
expressions.
TIP
Right-click a specific watch within the Watch window and select Delete
Watch from the context menu to delete it.
Adding Watch Values Using QuickWatch
If you are in Break mode and wish to view the contents of a variable or
expression in the current procedure, position your cursor on that variable
(or select the expression) and select Debug, QuickWatch. You will then see
a dialog box like the one in Figure 9.13, showing your watch
variable/expression. Click Add Watch to add a watch to the Watch window.
Figure 9.13. QuickWatch is great for looking up the value of an expression,
and it allows you to easily add a watch to the Watch window.
Call Stack Window
While in Break mode, Visual Studio .NET allows you to display the list of
procedures you have executed to get your current location. This is very
handy when you are not sure how you got to where you are. You can display
the Call Stack window, shown in Figure 9.14, by selecting Debug, Windows,
Call Stack.
Figure 9.14. The Call Stack window lets you see where you came from as you
debug an application.
TIP
The grayed-out procedures in the Call Stack window are procedures you
didn't call directly梩hey were called by the .NET Framework in order to get
to your code. You can double-click any of your own procedures in the Call
Stack window to have Visual Studio .NET highlight the procedure call within
your code.
Using the Command Window
The Command window (also known as the Immediate window) allows you to
perform simple calculations. Use the Debug, Windows, Immediate menu item to
display this window. You can also display or modify the value of a
variable. The statements you enter into the Command window may be any valid
Visual Basic expression. For example, you can print the results of an
operation, print the contents of a variable or property, or even set the
value of a variable or property. Use the "?" operator (which stands for
"print the value of the expression that follows") to display a value or to
run a function. Simply enter an assignment or method call that doesn't
return a value into the window to execute the expression. Figure 9.15 shows
the Command window in use.
NOTE
Unlike in Visual Basic 6.0, in Visual Studio .NET, you must be in Break
mode in order to enter text into the Command window. You'll also find other
differences. For example, you cannot delete lines in the Command window.
Right-click and use the Clear All context menu item to delete text from the
window.
Other Useful Debugging Windows
Visual Studio .NET provides even more windows and tools. Some of these are
tools you'll use every day; others are a bit more specialized and won't be
used as part of your daily routine.
Locals Window
If you wish to keep an eye on all the local variables in a procedure, you
can select Debug, Windows, Locals from the menu bar. This window, shown in
Figure 9.16, will display each of the variables in each procedure as you
work through the procedures in your debugging session. This window also
displays information about controls on the current page.
Figure 9.16. The Locals window shows you all your local variables.
Autos Window
If you just want to watch local variables, and nothing more, you can use
the Autos window (Debug, Windows, Autos). Figure 9.17 shows the Autos
window while debugging a simple procedure.
Figure 9.17. The Autos window shows all local variables.
TIP
You might be wondering where the name "Autos window" came from. It's one of
those C/C++/C# things梚n those languages, local variables are often called
auto variables (hence, the window name).
Me Window
The Me window, accessed via the Debug, Windows, Me menu item, displays all
the objects within the current class. Figure 9.18 shows the Me window in
use.
Figure 9.18. The Me window shows all objects and variables within the
current class.
TIP
The Locals, Autos, and Me windows are related. The Locals window shows a
union of the information provided by the Autos and Me windows梩hat is,
local variables and class variables.
Threads Window
The Threads window, accessed via the Debug, Windows, Threads menu item,
displays a list of all executing threads in your application. Most of the
time, in simple ASP.NET applications, you'll only be working with a single
thread. Figure 9.19 shows the Threads window for a simple debugging session.
Figure 9.19. The Threads window displays all executing threads.
Modules Window
The Modules window will show you all the modules that were loaded in order
to run an application. (This information is fun, if you're interested in
how things work "under the covers.") Display this window using the Debug,
Windows, Modules menu item. Figure 9.20 shows the Modules window for the
sample application.
Figure 9.20. The Modules window shows you all loaded modules.
Disassembly Window
If you like digging into the bits and bytes of your code, check out the
Disassembly window by selecting Debug, Windows, Disassembly. This may be
more information than you ever wanted to know about your code, but it's all
there for your entertainment and education. Should you ever really want to
know what Visual Basic .NET is doing under the covers, here's how you can
find out! Figure 9.21 shows the disassembly of some simple VB .NET code.
Figure 9.21. The Disassembly window shows you the assembly language version
of your code.
Registers Window
The Registers window, shown in Figure 9.22, will display what is in the
registers of the machine after the current instruction in your .NET
application. Use the Debug, Windows, Registers menu item to display this
window. (We doubt most Visual Basic .NET developers will ever take
advantage of this window梬e've included it here for completeness only.)
Figure 9.22. The Registers window shows you each register of your computer.