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
selecting the Build and Browse or View With… context menu item will not
allow you to debug. In addition, before you can debug an ASP.NET project,
you must set the project properties correctly. Right-click the project in
the Solution Explorer, select Properties from the context menu, and verify
that on the Configuration Properties, Debugging page, the ASP.NET Debugging
check box is selected. Otherwise, you'll never be able to debug the current
project.
Stepping into Code
Single-step mode executes each statement in your code, one at a time. You
can step through code by clicking the Step Into button on the Debug toolbar
or by selecting Debug, Step Into.
If, while debugging, you encounter a user-defined procedure during
single-step execution, you will single-step into each statement within that
procedure before returning to the called procedure.
Stepping over Code
Stepping over code is very similar to stepping into code, except that
rather than executing every statement within a called procedure, stepping
over code executes the entire procedure, while your debugging session never
leaves the current procedure. This allows you to quickly step over
procedures that you've already determined to be error free.
Use procedure step mode by clicking the Step Over toolbar button or
selecting Debug, Step Over.
TIP
We're not going to attempt to describe the keystrokes associated with each
debugging toolbar item, because the keystrokes depend on the profile you've
selected. Instead, check the Debug menu for each item and determine the
keystrokes from the tips provided on the menus.
WARNING
If you are moving from Visual Basic 6.0, you may be used to changing your
code while in Break mode and then continuing with these changes in place.
Visual Studio .NET doesn't make this possible, at least not in this first
release. You must stop your application, make your changes, and then
restart your debugging session in order to test your changes.
Entering Break Mode
When your application is running, you may invoke the debugger and enter the
Break mode in several ways:
Press Ctrl+Break while your code is running.
Place a Stop statement in your code. (Be wary of this technique! We'll have
more discussion of this later in the chapter.)
Set a breakpoint in your code.
Select Debug, Step Into to run the program.
The Stop Statement
If you want to place a permanent breakpoint into your code, place a Stop
statement at the location where you want the breakpoint. You'll want to
make sure you remove any Stop statements prior to distributing your
application梕ven when you create the release version of your application,
Stop statements drop into Break mode (or they halt the application, if the
debugger isn't available).
One good way to make sure any Stop statements are removed from your code at
release time is to always surround them with conditional compilation
directives that only include the statements if you've built a Debug build,
like this:
Private Sub BreakPointsSample()
Dim intValue As Integer
#If DEBUG Then
Stop
#End If
intValue = 10
Proc1()
End Sub