Click Here!
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.

Visual Basic 6 Black Book
(Publisher: The Coriolis Group)
Author(s): Steven Holzner
ISBN: 1576102831
Publication Date: 08/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Debugging In Visual Basic

Visual Basic offers a powerful suite of debugging options—notably the ability to single-step through your code as it’s executing. The standard way to debug is to place a breakpoint at a particular line in your code, and when execution reaches that line, it will halt and Visual Basic will enter the Debug state, giving you access to your code and variables. You can examine the contents of those variables and work through your code line by line, watching program execution behind the scenes.

For example, we might write the following code, which is meant to increment the value in a text box, Text1, each time you click a button:

Private Sub Command1_Click()
    Dim intCounter As Integer

    intCounter = intCounter + 1
    Text1.Text = intCounter

End Sub

What actually happens is that the value 1 appears in the text box each time you click the button—it’s time to debug. To start that process, we place a breakpoint at this line in our code:

Private Sub Command1_Click()
    Dim intCounter As Integer

    intCounter = intCounter + 1
    Text1.Text = intCounter
End Sub

You place a breakpoint in code by moving the text insertion caret to that line and either selecting Toggle Breakpoint in the Debug menu or pressing F9. (Breakpoints toggle, so to remove the breakpoint, select Toggle Breakpoint in the Debug menu or press F9 again.)

Now when you run the program and press the button, execution halts at the breakpoint, and the code window appears. You can examine the contents of variables by selecting them on the screen with the mouse and clicking the Quick Watch item in the Debug menu (besides individual variables, you can select an entire expression). This opens a window displaying the current value of the variable or expression you’ve selected.

To move through your program step-by-step, you can select these stepping options in the Debug menu:

  Step Into—Single-step through the code, entering called procedures if encountered.
  Step Over—Single-step through the code, stepping over procedure calls.
  Step Out—Step out of the current procedure.

Examining the contents of the intCounter variable shows that it’s being reset to 0 each time the code runs, and we realize that we should declare that variable as static this way:

Private Sub Command1_Click()
    Static intCounter As Integer

    intCounter = intCounter + 1
    Text1.Text = intCounter

End Sub

That’s the process in overview—we’ve debugged the code. For more details on the debugging process, and to examine other debugging processes like selected code execution, see the following topics in this chapter.


TIP:  Want to save a little time debugging? Often the crucial aspect of debugging is watching the values in your variables change as your program executes, and sometimes doing that without the debugger is easy enough: just add some temporary text boxes or message boxes to your program and use them to display the values you want to watch. This is an expedient shortcut for simple bugs—but if it doesn’t fit the bill for you, turn to the debugger.

Setting Debugging Breakpoints

Breakpoints are the foundation of Visual Basic debugging. When you set breakpoints in a program and run that program, program execution continues until one of the breakpoints is encountered and program execution stops, making Visual Basic enter the Debug state. You place a breakpoint in code by moving the text insertion caret to that line and either selecting Toggle Breakpoint in the Debug menu or pressing F9. (Breakpoints toggle, so to remove the breakpoint, select Toggle Breakpoint in the Debug menu or press F9 again.) When you place a breakpoint in code, it appears at design time in red, as shown in Figure 29.3.


Figure 29.3  Setting a breakpoint in code at design time.

When you run the program and reach the breakpoint, execution stops and Visual Basic appears in the Debug state, as shown in Figure 29.4. You can see the breakpoint highlighted in Visual Basic in that figure, and the arrow in the left margin of the code window points to the current line of execution.


Figure 29.4  A breakpoint in Visual Basic’s Debug state.

You can execute the lines following a breakpoint by single stepping, and we’ll take a look at that process in the next topic.

Single-Stepping While Debugging

When you’ve stopped a program at a breakpoint, you get an inside look at what’s going on. When the code is stopped at a breakpoint, you can move through the code with the single-stepping options in the Debug menu:

  Step Into—Single-step through the code, entering the code in called procedures if procedure calls are encountered (shortcut: press F8).
  Step Over—Single-step through the code, stepping over procedure calls (shortcut: press Shift+F8).
  Step Out—Step out of the current procedure (shortcut: press Ctrl+Shift+F8).

For example, take a look at Figure 29.4. There, we are stopped at a breakpoint, but we can single-step to the next line, as shown in Figure 29.5. As you can see in that figure, the arrow at left in the code window has moved to the next line, and we’ve executed the previous line of code. Single stepping in this way, you can move through your code to debug it.


Figure 29.5  Single-stepping in Debug mode.

Examining Variables And Expressions

Just single-stepping through a program when debugging it wouldn’t be of that much use—we should be able to examine the value in the various program variables as well. You can do that with a “quick watch” window.

To examine the value in a variable or expression in the code window when stopped at a breakpoint, select the variable or expression you want to examine with the mouse and select the Quick Watch item in the Debug menu, or press Shift+F9. This opens the Quick Watch window you see in Figure 29.6. You can see the value in the variable named intCounter in that window.


Figure 29.6  Examining a variable with a Quick Watch window.

Besides quick watches, you can open a whole watch window, as we’ll see in the next topic.


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.