|
To access the contents, click the chapter and section titles.
Sams Teach Yourself Visual J++ 6 in 21 Days
BreakpointsBreakpoints are places in the source code where you specify that the debugger must pause execution until you continue (by pressing F5, F10, F11, or any other navigation shortcut key). Breakpoints give you the opportunity to examine variables at a given point in the program. For instance, if somewhere in the calculateValue() method a variable is becoming negative when it should always be positive, you can set a breakpoint right before the variable is altered and watch what happens when its value changes. To set a breakpoint, simply click in the gray margin of the source-code window. A small red circle will appear, indicating that a breakpoint is set at that location. You can also set a breakpoint wherever the cursor is by pressing the F9 key. Clearing breakpoints is done the same way as setting them. Just click on the red breakpoint circle, and it will go away. You can also press F9 when the cursor is on a line with a breakpoint, and it will go away. Okay, lets practice. Add the method given in Listing 4.1 to your LearnToDebug application, and then call it from your Form1 constructor after the initForm() method is called. Listing 4.1 A Method That Will Help You Learn to Debug 1 void practiceMethod() 2 { 3 int i, j, k; 4 int nValue = 0; 5 int nCalculate = 0; 6 7 for( i=0; i<10; i++ ) 8 { 9 for( j=0; j<15; j++ ) 10 { 11 for( k=0; k<15; k++ ) 12 { 13 nValue = i * j + k; 14 if( j == 10 ) 15 nValue = -nValue; 16 else if( j ==5 && i == 4 ) 17 continue; 18 else if( k == 10 && i == 5 ) 19 break; 20 } 21 } 22 // Here is where we need to 23 // know the contents of nValue. 24 nCalculate = nValue - 1; // Set your breakpoint here. 25 } 26 27 } There are three for loops in the preceding code. It would take a long time to step through the code. Because all were concerned with is the value of nValue and the assignment to nCalculate, we simply have to set a breakpoint on the line indicated in the source code at line 25. Every time the debugger gets to line 25, program execution will be paused. After you examine the contents of nValue, you can use the Continue command (F5) to resume execution. One point to note: At the line at which the breakpoint is set, the nCalculate variable hasnt been set. To allow this variable to be set, youd have to single-step with the Step Into command (actually, Step Over would work also).
After running through the program, youll get values as shown in Table 4.3.
The Breakpoints Dialog BoxThe Breakpoints dialog box shown in Figure 4.6 displays a list of all breakpoints in the current solution. To bring up the breakpoints dialog box, press Ctrl+B or select Debug, Breakpoints. You can use the Breakpoints dialog box to add a new breakpoint by name only. You can also enable, disable, or remove one or more existing breakpoints by pressing Ctrl and clicking on the breakpoint or breakpoints. And you can modify the properties of a breakpoint.
Several options are available in the Breakpoints dialog box. Table 4.4 shows the options.
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |