Using the Debugger
Visual Studio .NET provides a useful toolbar you can use while debugging but
doesn't display it by default. In this section, you'll see both how to load
the Debug toolbar and how to get started debugging.
Three Modes of Existence
While creating applications in Visual Studio .NET, you'll find yourself in
one of three "modes." Each mode has a specific function, and knowing which
mode you are in is important. The three modes are as follows:
Design mode. In this mode you build your user interface and write your code.
Break mode. In this mode you can single-step through code, view and modify
variable values, and inspect memory.
Run mode. In this mode you are, effectively, testing your application
within the Visual Studio .NET environment.
You can determine which mode you are in by looking at the Visual Studio
.NET title bar. At the end of the other information in the title bar, you
will see one of the following: [design], , or [break]. Get used to looking
at the title bar for information on what mode you are currently in. There
are things you can and can't do in each mode.
TIP
What can't you do in design mode? You can't use the Immediate window to
test out code or expressions as you could in VB6. In break mode, you can't
modify code, as you could in VB6. If you're coming to VB .NET from VB6, you
may find some of these new limitations frustrating and confusing.
It is Break mode we'll focus on in this chapter, because it's in Break mode
that you'll be able to single-step through code and debug the code as it
runs.
Finding the Debug Toolbar
Visual Studio .NET provides many options for debugging your code, and
although you can use menu items or keyboard shortcuts, you may find the
Debug toolbar the easiest way to work. To make sure the Debug toolbar is
visible, right-click the toolbar area of the Visual Studio .NET IDE and
then select the Debug toolbar from the list of available toolbars. Once
you've made the selection, you will see the toolbar shown in Figure 9.1.
Figure 9.1. The Debug toolbar gives you most of the debugging functionality
without needing to dig into keyboard shortcuts or menu items.
From this toolbar, you will be able to run, stop, and pause the
application. You will also be able to perform other functions related to
debugging your application. From the left, the tools are as follows:
Start. Runs the project.
Break All. Pauses the project.
Stop Debugging.
Restart. Continues running the application from the current location.
Show Next Statement. Jumps to the next statement that will be executed
while single-stepping through code.
Step Into. Single-steps into procedures in your code.
Step Over. Single-steps, running procedures full speed.
Step Out. Runs full speed from the current location back to the procedure
that called the current procedure.
Hexadecimal Display. Toggles the display of all values in the QuickWatch
window between decimal and hexadecimal formats.
Breakpoints/Immediate. Displays and then toggles between displaying the
Breakpoints and Immediate windows.
You will learn more about these and other tools in this chapter.
TIP
While in Break mode, you have other tricks up your sleeve, as well. You can
select the next line of code you'd like to execute, for example, by simply
dragging the current line indicator to a different line (within the same
procedure). You'll find debugging in Visual Studio .NET to be a very
powerful experience!
Invoking the Debugger
To begin debugging your application, select Debug, Step Into or the Debug,
Step Over menu options (or the corresponding toolbar items). The hotkeys
for each of these may be different, depending on how you customized your
profile when you first ran Visual Studio .NET. For example, if you are set
up as a Visual Studio developer, the hotkeys for these two commands will be
F11 and F10, respectively. If you are set up as a Visual Basic developer,
however, you will use F8 and Shift+F8, respectively.
TIP
No matter how hard you try, debugging from within an ASP.NET application
simply doesn't work unless you press F5 (or use the Debug, Start menu item)
to begin running your code. (Your application must have a start page in
order for this to work. Right-click the correct page, in the Solution
Explorer window, and select Set as Start Page from the context menu.) There
are other ways to accomplish the same goal, but this is how we've gotten
debugging to work. Certainly, selecting a page, right-clicking, and