Sams Teach Yourself Visual Basic 6 in 24 Hours

Sams Teach Yourself Visual Basic 6 in 24 Hours

By Greg Perry

Setting Breakpoints

You'll always enter break mode from the runtime mode. Only after you begin a program's execution will the break mode be available, because only at runtime are the variables and controls initialized with values. Here are the ways that you can move from runtime mode to break mode:

The most accurate and common way to enter break mode is by setting a breakpoint. To set a breakpoint, find the line where you want execution to halt at a breakpoint and set a breakpoint at that particular line of code. The following steps walk you through setting a breakpoint:

  1. Load the Controls.vbp project that comes with Visual Basic (look in the Samples folder).
  2. Press F7 to open the Code window.
  3. Locate the opt486_Click() event procedure.
  4. Find the following line of code in opt486_Click():
    strComputer = "486"
    
  5. Move the mouse cursor to the line and click the mouse button. The text cursor appears at the mouse click's location.
  6. Select Debug | Toggle Breakpoint to set a breakpoint. (You'll see from this menu bar command that F9 is the shortcut key for this command. Also, clicking the toolbar's hand icon would place a breakpoint on this line of code as would clicking to the left of the line.) Figure 20.6 shows how your Code window should appear. Visual Basic changes the color of the line to let you know that a breakpoint will take place on that line during the program's execution.
    20fig06.gif

    Figure 20.6 The breakpoint line is highlighted.

    You can turn off a breakpoint by selecting Debug | Toggle Breakpoint (or by pressing F9) once again. You can set as many breakpoints as you need throughout a program. Leave this breakpoint in place for now. By setting the breakpoint, you're requesting that Visual Basic halt the program and enter break mode when execution reaches this line of code. Close the Code window and run the program by pressing F5.

The program appears to run as usual. The opening dialog box appears. Click the Option Buttons command button to see what happens when execution reaches the breakpoint. The execution continues, as usual, as long as the breakpoint isn't reached, but when the breakpoint line is reached, execution halts.

As soon as Visual Basic reaches a breakpoint's line, Visual Basic enters break mode before executing the breakpoint line. The opt486_Click() event procedure assigns a string literal to a string variable and then calls another procedure to load that variable into a label. The breakpoint that you set occurs in the middle of the assignment code.

Follow these steps to see what kinds of things you can do at a breakpoint:

  1. Move your mouse to the string variable. After a brief pause, a ToolTip-like message pops up to tell you that the variable contains a null string value (nothing is yet assigned to the string). Drag the mouse to highlight the strComputer variable on the breakpoint's line.
  2. Select Debug | Quick Watch. The menu option produces the Quick Watch dialog box. Visual Basic displays the breakpoint line, the variable name, and the current value that's a null string, as shown in Figure 20.7.
    20fig07.gif

    Figure 20.7 Looking at the variable's null value.

  3. Click Add to add the value to the Watch dialog box. Whereas the Quick Watch dialog box is useful for looking at a variable at its current location, the Watch dialog box keeps track of multiple variables that update as the program executes.
  4. Select View | Toolbars | Debug to display the special floating Debug toolbar. Most tools you need for interactive debugging appear on this toolbar. As Figure 20.8 shows, the Debug toolbar includes its own Quick Watch button. In addition, Figure 20.8 shows you the Watches window, where the variables and controls you want to watch reside.
    20fig08.gif

    Figure 20.8 Looking at the variable in the Watches window.

    The difference between the Quick Watch window and the Watches window is that you can, at any breakpoint, highlight a variable and display its value and surrounding code and datatype by clicking the Debug toolbar's Quick Watch button. If you want to keep a running list of watch variables, however, you must add the variables and controls to the Watches window. If you start the program again and hit another breakpoint later (you can set multiple breakpoints), the Watches window still shows the variables and controls you placed there, but the Quick Watch window will no longer appear until you request it again with another highlighted value. When you single-step through code, you execute subsequent program instructions, one statement at a time, looking at values and testing the logic as you go.
  5. Usually, the programmer will single-step through a few lines of code after reaching a breakpoint. To step through the code one line at a time, you can choose the Debug | Step Into option (or press F8). As you single-step though the code, Visual Basic highlights the next line of execution. At any point during the single-step process, you can examine variables and controls with the Quick Watch dialog box and add them to the Watches window.

Share ThisShare This

Informit Network