Tracing provides you with a snapshot of the steps your code has taken after the code has run. At times, however, you'd probably like to monitor your code while it is running. What you want is more of a CAT scan than an autopsy. The code equivalent of a CAT scan is a symbolic debugger.
When you run your code in the debugger, you can literally watch your code work, step by step. As you walk through the code, you can see the variables change values, and you can watch as objects are created and destroyed.
This section will provide a brief introduction to the most important parts of the debugger that accompanies the Visual Studio .NET IDE. For complete coverage of how to use the Visual Studio .NET debugger, we urge you to spend time with the documentation and to experiment freely. The debugger is one of the most powerful tools at your disposal for learning ASP.NET.
7.3.1 The Debug Toolbar
There is a Debug toolbar available in the IDE. To make it visible, click on the View/Toolbars menu commands, then click on Debug, if it is not already checked. Table 7-3 shows the icons that appear on the Debug toolbar.
Breakpoints are at the heart of debugging. A breakpoint is an instruction to .NET to run to a specific line in your code and then to stop and wait for you to examine the current state of the application. While the execution is paused, you can:
188.8.131.52 Setting a breakpoint
A breakpoint is set in the Source window (any Source window, such as page file, control file, or code-behind) by single-clicking on the gray vertical bar along the left margin of the window. A red dot will appear in the left margin and the line of code will be highlighted, as shown in Figure 7-6.
An alternative to clicking in the left margin is to select the Debug/New Breakpoint... menu command. Clicking on the File tab will bring up the dialog shown in Figure 7-7. The text boxes will already be filled in with the current location of the cursor.
184.108.40.206 Breakpoint window
A Breakpoint window is shown in Figure 7-8.
You can toggle a breakpoint between Enabled and Disabled by clicking on the corresponding checkbox in the Breakpoint window.
220.127.116.11 Breakpoint properties
Sometimes you don't want a breakpoint to stop execution every time the line is reached. VS.NET offers two properties that can be set to modify the behavior of a breakpoint. These properties can be set in either of two ways:
In either case, you will see the dialog box shown in Figure 7-9.
The fields at the top of the Breakpoint Properties dialog box will default to the location of the current breakpoint. The two buttons allow access to the Condition and Hit Count properties.
The Condition button brings up the dialog shown in Figure 7-10.
You can enter any valid expression in the edit field. This expression is evaluated when program execution reaches the breakpoint. Depending on which radio button is selected and how the Condition expression evaluates, the program execution will either pause or move on. The two radio buttons are labeled:
18.104.22.168.2 Hit count
Hit count is the number of times that spot in the code has been executed since either the run began or the Reset Hit Count button was pressed. The Hit Count button brings up the dialog shown in Figure 7-11.
Clicking on the drop-down list presents the following options:
If you click on any option other than "break always" (the default), the dialog box will add an edit field for you to enter a target hit count.
Suppose this is a breakpoint set in a loop of some sort. You selected "break when the hit count is a multiple of" and entered 5 in the edit field. Then the program will pause execution every fifth time it runs.
22.214.171.124 Breakpoint icons
There are several different breakpoint symbols, or glyphs, each conveying a different type of breakpoint. These glyphs appear in Table 7-5.
126.96.36.199 Stepping through code
Go to the code-behind file in the example (either WebForm1.aspx.cs or WebForm1.aspx.vb, depending on your language). Place a breakpoint on the call to the Add method of the DropDownList control's Items collection, the line in the Page_Load method where the items are added to the DropDownList. Set the Hit Count to be a multiple of 5 (break always when hit count is a multiple of 5). Then run the program.
The breakpoint will be hit, and the program will stop execution at the line of code containing the breakpoint, which will turn yellow. The glyph in the left margin will have a yellow arrow on top of it. The VS.NET screen should look like Figure 7-12.
You can now move forward a single statement or line at a time, stepping into any methods or functions as you go, by using one of the following techniques:
Finally, if you are debugging in a called method or function, you can step out of that method or function call, using one of the following techniques:
To change the granularity of the stepping (that is, step by Line, Statement, or Function), select a different value for Step By, either in the Debug Toolbar or at the Debug/Step By menu command.
188.8.131.52 Examining variables and objects
Once the program is stopped, you can examine the value of objects and variables currently in scope. This is incredibly intuitive and easy. Just place the mouse cursor over the top of any variable or object in the code, wait a moment, and a little pop-up window will appear with its current value
If the cursor is hovering over a variable, the pop-up will contain the type of variable, its value (if relevant), and any other properties it may have.
If the cursor is hovering over some other object, the pop-up window will contain information relevant to its type, including its full namespace and syntax, and a descriptive line of help.
184.108.40.206 Immediate window
You can enter expressions for immediate execution in the Immediate window. If you want to see the value of an expression, prepend it with a question mark. For instance, if the breakpoint is on the line shown in Figure 7-12, you can see the value of the integer i by entering:
in the Immediate window and pressing Enter. Figure 7-13 shows the result of that exercise; additionally, this figure shows the process of assigning a new value to the variable i and then viewing its value again.
You can clear the contents of the Immediate window by right-clicking anywhere in the window and selecting Clear All. Close the window by clicking on the X in the upper-right corner. If you close the window and subsequently bring it back up in the same session, it will still have all the previous contents.
220.127.116.11 Autos window
A typical Autos window is shown in Figure 7-14.
There are columns for the name of the object, its value, and its type. A plus sign next to an object indicates that it has child objects that are not displayed while a minus sign indicates that its child objects are visible. Clicking on a plus symbol expands out the tree and shows any children, while clicking on a minus symbol contracts the tree and displays only the parent.
You can select and edit the value of any variable. The value will then display as red in the Autos window. Any changes to values take effect immediately.
18.104.22.168 Locals window
The Locals window is exactly the same as the Autos window, except that it shows variables local to the current context. By default, the current context is the method or function containing the current execution location.
To open a Locals window, do any of the following:
22.214.171.124 This/Me window
To open a This/Me window, do any of the following:
126.96.36.199 Watch window
The Watch window is exactly the same as the Autos window, except that it shows only variables, properties, or expressions that you enter into the Name field in the window or drag from another window. The advantage of using a Watch window is that it allows you to watch objects from several different source windows simultaneously. This overcomes the inability to add object types other than the specified type to any of the other debug windows.
To open a Watch window, do any of the following:
In addition to typing in the name of the object you want to watch, you can also drag and drop variables, properties, or expressions from a Code window. Select the object in the code that you want to put in the Watch window, then drag it to the Name field in the open Watch window.
You can also drag and drop objects from any of the following windows into the Watch window:
In order to drag something from one of these windows to the Watch window, both the source window and the Watch window must be open. Highlight a line in the source window, then drag it down over the Watch tab. The Watch window will come to the foreground. Continue dragging the object to an empty line in the Watch window.
188.8.131.52 Call Stack window
The Call Stack window displays the names of the methods and functions on the call stack, as well as their parameter types and values. You can control which information is displayed in the Call Stack window by right-clicking anywhere in the window and toggling field names that appear in the lower portion of the pop-up menu. To open a Call Stack window, do any of the following:
184.108.40.206 Threads window
The Threads window allows you to examine and control threads in the program you are debugging. Threads are sequences of executable instructions. Programs can be either single-threaded or multithreaded. The whole topic of threading and multiprocess programming is beyond the scope of this book.
To open a Threads window, do any of the following:
220.127.116.11 Modules window
A Modules window is shown in Figure 7-15.
By default, the modules are shown in the order in which they were loaded. You can re-sort the table by clicking on any of the column headers.
18.104.22.168 Disassembly window
The Disassembly window shows the current program in assembly code. If you are debugging managed code, such as that which comes from VB.NET, C#, or Managed C++, this will correspond to Microsoft Intermediate Language (MSIL) code.
A Disassembly window is shown in Figure 7-16.
Unlike the previous windows discussed in this chapter, the Disassembly window displays as a tabbed item, as part of the main Source code window. You can set breakpoints anywhere in the window, just as for any other Source code window. To open a Disassembly window, do any of the following:
22.214.171.124 Registers window
You can select which pieces of information to view by right-clicking anywhere in the Registers window and clicking on the information you would like displayed.
126.96.36.199 Memory windows
There are four Memory windows available for viewing memory dumps of large buffers, strings, and other data that will not display well in any other window. To open a Memory window, do any of the following:
An application can be configured to either enable or disable debugging. This is done through the web.config file, which is described more fully in Chapter 200.
Web.config is an XML file, and as such it must be well-formed. The file consists of sections delimited by tags. The debugging configuration information is contained within the <compilation> section, within the <system.web> section, which in turn is contained within the <configuration> section. So a typical compilation configuration snippet will look something like Example 7-8.
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.web> . . . <compilation debug="true" />
Note that setting debug to false improves the runtime performance of the application.