Click here for ObjectSpace: Business- to- Business Integration Company
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Sams Teach Yourself Visual J++ 6 in 21 Days
(Publisher: Macmillan Computer Publishing)
Author(s): Rick Leinecker
ISBN: 0672313510
Publication Date: 11/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Breakpoints

Breakpoints 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, let’s 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 we’re 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 hasn’t been set. To allow this variable to be set, you’d have to single-step with the Step Into command (actually, Step Over would work also).


Tip:  In a situation like this, when there’s no code following a variable assignment at which to set a breakpoint, you can add some code just so that you have the opportunity to set a breakpoint after the line nCalculate = nValue - 1; (line 25). In the preceding situation, I would add a local variable at the top named m and the code m = 0; below the nCalculate = nValue - 1; line (which would become line 26) so that there’s a place to set a breakpoint after nCalculate is set.

After running through the program, you’ll get values as shown in Table 4.3.

Table 4.3 Values During the Debug Session

i nValue nCalculate

0 14 13
1 28 27
2 42 41
3 56 55
4 70 69
5 80 79
6 98 97
7 112 111
8 126 125
9 140 139

The Breakpoints Dialog Box

The 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.


Figure 4.6   The Breakpoints dialog box offers an easy way to manage your breakpoints.

Several options are available in the Breakpoints dialog box. Table 4.4 shows the options.

Table 4.4 The Breakpoints Dialog Box Options

Option Description

Breakpoints Lists all the breakpoints in the current solution
Properties Displays the Breakpoint Properties dialog box, where you can set the properties of the selected breakpoint
View Code Closes the Breakpoints dialog box after saving the changes, and displays the selected breakpoint in the Text Editor window
Select All Selects all the breakpoints in the Breakpoints list
Enable Enables the selected breakpoints
Disable Disables the selected breakpoints
Remove Deletes the selected breakpoint or breakpoints


Previous Table of Contents Next


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.