Section 10.1. Setting a Breakpoint

   

10.1 Setting a Breakpoint

To get started with the debugger, return to Example 9-1. To see how this code actually works, you'll put a breakpoint on the first line of Main( ). A breakpoint is an instruction to the debugger to stop running. You set a breakpoint and then run the program. The debugger will run the program up until the breakpoint. Then you will have the opportunity to examine the value of your variables at this point in the execution. Examining your program as it runs can help you untangle otherwise impenetrable problems.

It is common to set multiple breakpoints. This allows you to zip through your program, examining the state of your object at selected locations.

You can set a breakpoint in many different ways. The easiest is to click in the far-left margin. This causes a red dot to appear in the margin next to the relevant line of code, which is also highlighted in red, as shown (in black and white) in Figure 10-1. Notice that as you hover over the breakpoint, it tells you the line on which the breakpoint appears.

Figure 10-1. Setting a breakpoint
figs/lvbn_1001.gif

You are now ready to run the program to the breakpoint. Again, there are a number of ways to do so. You can click on the Start button (see Figure 4-6); or you can choose the Start item from the Debug menu (or use the keyboard shortcut for the menu item, the F5 key). In any case, the program will start and will run to the breakpoint, as shown in Figure 10-2.

Figure 10-2. At the breakpoint
figs/lvbn_1002.gif

The next statement to be executed is highlighted (in this case, the initialization of the currentTime object). There are a number of other helpful windows open as well, which will be examined in detail.

To step into the code, press the F11 function key twice. With the first keypress, the currentTime object is initialized . The second keypress moves you to the next line in the code, which initializes a second Time object called time1. (This line is also called the Time constructor.)

F11 and F10 are the step commands. The difference is that F10 will step over method calls, while F11 will step into them.

The methods are executed with F10; you just don't see each step in the debugger. The highlighting jumps to the next statement past the method call.

If you use F11 to step into a method you meant to step over, Shift-F11 will step you out. The method you stepped into will run to completion, and you'll break on the first line back in the calling method.

Press the key seven more times to initialize each of the member variables. You can see their values being set in the so-called Autos window, in the lower-left corner of the debugger, as shown in Figure 10-3 .

Figure 10-3. The Autos window close up
figs/lvbn_1003.gif

10.1.1 Using the Debug Menu to Set Your Breakpoint

Rather than clicking in the margin to set your breakpoint, you can use the New Breakpoint item on the Debug menu (or use the keyboard shortcut for the menu item, Control-B). This brings up the New Breakpoint dialog box, as shown in Figure 10-4.

Figure 10-4. The New Breakpoint dialog
figs/lvbn_1004.gif

The New Breakpoint dialog allows you far greater control over your breakpoint. You can set it to break only when a specific condition is hit (e.g., when counter > 10).

You can also set the hit count to designate that you want the debugger to break in only when the line has been hit a specified number of times (or a multiple of a specific number, etc.), as shown in Figure 10-5.

Figure 10-5. Breakpoint hit count
figs/lvbn_1005.gif

This can be very useful when you are in a loop (as described in Chapter 6). Rather than breaking each time through a loop of 100 iterations, you can choose the conditions under which to break.

You can also examine and manipulate all the breakpoints together in the Breakpoints window, as shown in Figure 10-6.

Figure 10-6. The Breakpoints window
figs/lvbn_1006.gif

10.1.2 Examining Values: The Autos and Locals Windows

Look at the bottom lefthand windows, where your variables are displayed. These variables are organized in a tabbed set of windows named Autos and Locals. You've already had a sneak peek at the Autos window, back in Figure 10-3.

The debugger will stack the Autos and Locals windows together with tabs as shown in Figure 10-7. You are free to separate these windows or to move them to be tabbed with other windows. You can simply drag and drop the windows where you want them. When you drop one window on another, the two windows are tabbed together.

The Autos and Locals windows can be used to display the current value of each variable (and parameter) in your program. The Autos window shows variables used in the current statement and the previous statement. (The current statement is the statement at the current execution location, which is highlighted automatically in the debugger ”thus the window's name .) The Locals window displays all objects currently in scope (that is, in the current method), also allowing you to see the object's member fields and variables.

Figure 10-7 shows the Locals tab selected to display the Locals window. The Locals window is showing you that the local variable currentTime has been set to the current date. (Since the value of currentTime has just been set, it will appear in red.) The window also shows you the two objects time1 and time2.

Figure 10-7. The Locals window on top
figs/lvbn_1007.gif

Notice the plus sign (+) next to the time1 object. This object is of type System.Time, which turns out to be a type with many member variables. Expanding the plus sign reveals the state of this object, as shown in Figure 10-8.

Figure 10-8. Expanding the object reveals its member variables
figs/lvbn_1008.gif

Explore the Locals and Autos windows as you step through the program. When you want to stop, choose the Stop debugging item from the Debug menu to stop processing and return to the editor.

10.1.3 Set Your Watch

In a program with many local variables, it can be difficult to keep track of the particular variables you want to keep an eye on. You can track variables and objects in the Watch window. You can have up to four Watch windows at a time. Watch windows are like by- invitation versions of the Locals window; they will list the objects you ask the debugger to keep an eye on, and you can see their values change as you step through the program, as illustrated in Figure 10-9.

Figure 10-9. A Watch window
figs/lvbn_1009.gif

The Watch windows are usually tabbed with the Locals window. You might create more than one Watch window to organize the variables you keep an eye on.

You can add a watch by right-clicking on a variable and choosing Add Watch. You might instead choose Add QuickWatch, which opens a dialog box with watch information about a single object, as shown in Figure 10-10.

Figure 10-10. QuickWatch
figs/lvbn_1010.gif

From within the QuickWatch window, you can enter any expression and evaluate it. For example, suppose you had integer variables named varOne and varTwo:

 Dim varOne As Integer = 5 Dim varTwo As Integer = 7 

If you want to know the impact of multiplying them, you can just enter:

 varOne * varTwo 

into the Expression window and click Recalculate. The value is shown in the Current value window, as in Figure 10-11.

Figure 10-11. QuickWatch recalculation
figs/lvbn_1011.gif
   


Learning Visual Basic. NET
Learning Visual Basic .Net
ISBN: 0596003862
EAN: 2147483647
Year: 2002
Pages: 153
Authors: Jesse Liberty

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net