VB6 beginners tutorial - Learn VB6

Advanced VB6 tutorial - Learn Advanced VB6

Systems Analysis - System analysis and Design tutorial for Software Engineering

You are here: Visual Basic > Advanced VB6 tutorial > Chapter 18

Stepping Through Your Code

Setting breakpoints usually isn’t an exact science. While tracking down a bug, you may observe that problems occur after clicking on a certain command button. That doesn’t necessarily mean that the Click event code in the command button is at fault, of course. You don’t have to wait to pin down the problem more closely before setting a breakpoint, however. Because you know that the problem occurs at some point after that procedure executes, go ahead and set a breakpoint there so that you are at least that much closer to the problem when you are in Break mode.

Because program execution is suspended during Break mode, you may wonder how this helps you get closer to the bug. After all, you haven’t reached the bug yet, and the program has stopped. How do you get to the problem?

Besides enabling you to watch variable and property values during Break mode, VB also enables you to continue to execute the program on an incremental basis. The process is typically called stepping through your code, and it enables you to execute a program a line at a time. You may not be right at the point at which a problem occurs, but you can step through your code to sneak up on the bug and catch it in the act.

The following four stepping commands are available in Break mode:

The stepping commands are available in several ways: from the Debug menu, from the keyboard via the F8 key (by itself, or in conjunction with the Shift, Control, and Alt keys), or from the Debug toolbar—as shown in Figure 18.8. If the Debug toolbar is not already visible, you can pull down the View menu and choose Toolbars, where you can toggle it off and on. The Debug toolbar can be docked or permitted to float.

The Debug toolbar.
FIGURE 18.8 The Debug toolbar.

Here is a simple example to illustrate how the step commands behave after a program is in Break mode. Consider a project containing one form with a single command button. The code looks like this:

Sub cmdCommand_Click()
Dim strName as String
Msgbox "After this message, break mode begins."
strName = MyFunction()
End Sub
Function MyFunction() as String
Dim strPrompt as String
strPrompt = "Please enter your name"
MyFunction = InputBox (strPrompt)
End Function

When you run this project and click on the command button, the program enters Break mode when it reaches the Stop command. The code editor will appear with the Stop command highlighted in the cmdCommand_Click function. Program execution is suspended, and nothing else happens until you issue a step command.

Step Into

If you select Step Into, the highlight moves to the next line of code, where strName is assigned the value returned by MyFunction. If you select Step Into again, the code editor will display the code for MyFunction with the highlight in its first line (recall that the highlighted line hasn’t executed yet; it runs after you perform the next stepping action). If you continue to select Step Into, you will see where the command gets its name: Step Into enables you to step into any code that is called in your project, executing every procedure a line at a time.

Step Over

Step Over is similar to Step Into. Within the current procedure, Step Over continues to execute the code one line at a time. The difference is how it handles calls to other procedures. If you run this project again and select Step Over when you reach the strName = MyFunction() line in the Click event, you won’t step into the MyFunction code. Instead, it runs all at once and you are returned to the function that called it. When you aren’t interested in observing the behavior of another procedure called from within the one you are debugging, choose Step Over.

Step Out

What if you selected Step Into, only to realize that you really don’t need to watch the execution of every step of the procedure you just stepped into? No, you don’t have to lean on the F8 key to force the execution of every line. That’s what Step Out is for; it causes the rest of the current procedure to run and returns to the calling procedure. That is, you could select Step Out as soon as you entered MyFunction, and you would find yourself back in the Click event just after MyFunction had completed its work.

Step to Cursor

You may step into a procedure that only has a few lines that you want to watch execute. If that happens, and there are a lot of lines from your point of entry to the lines that interest you, you don’t need to wade through the boring parts with Step Into or Step Over. Instead, move the cursor to the first line you want to watch, and then select Step to Cursor. All the code will execute between the line at which you originally entered Break mode and the line where you placed the cursor, then you can begin to step through the interesting parts again.

Setting Stepping Options

You will also notice a couple of other options at the bottom of the Debug menu. If you want to prevent a few lines of code from executing, but otherwise let your program continue to execute normally, use Set Next Statement

While in Break mode, this enables you to move the cursor to the next line that you want your program to execute. Because this skips the intervening lines, use this feature carefully. If you inadvertently skip some lines that modify values in your program, you may not catch the bug you are after. If you aren’t sure what line is supposed to execute next, use Show Next Statement.

The Set Next statement option only works with lines of code that are inside the currently running procedure. In other words, you can’t set the next statement to a line of code in a different procedure from the currently running procedure.

To exit Break mode and resume the normal operation of your program, choose Continue from the Debug menu or toolbar. (Its keyboard equivalent is F5.) If you have seen all that you need to see, you can also halt the program by choosing End, which is available on the Debug menu or on the toolbar.


<< Previous | Contents | Next >>

Home | About Us | Privacy Policy | Contact Us

Copyright © | All Rights Reserved