|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
Debugging In Visual BasicVisual Basic offers a powerful suite of debugging optionsnotably the ability to single-step through your code as its 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 buttonits 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 youve selected. To move through your program step-by-step, you can select these stepping options in the Debug menu:
Examining the contents of the intCounter variable shows that its 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
Thats the process in overviewweve 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.
Setting Debugging BreakpointsBreakpoints 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.
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.
You can execute the lines following a breakpoint by single stepping, and well take a look at that process in the next topic. Single-Stepping While DebuggingWhen youve stopped a program at a breakpoint, you get an inside look at whats 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:
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 weve executed the previous line of code. Single stepping in this way, you can move through your code to debug it.
Examining Variables And ExpressionsJust single-stepping through a program when debugging it wouldnt be of that much usewe 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.
Besides quick watches, you can open a whole watch window, as well see in the next topic.
|
|
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. |