Section C.4. Controlling Execution Using the Step Into, Step Over, Step Out and Continue Commands


C.4. Controlling Execution Using the Step Into, Step Over, Step Out and Continue Commands

Sometimes you will need to execute a program line-by-line to find and fix logic errors. Stepping through a portion of your program this way can help you verify that a method's code executes correctly. The commands you learn in this section allow you to execute a method line-by-line, execute all the statements of a method or execute only the remaining statements of a method (if you have already executed some statements within the method).

1.

Setting a breakpoint. Set a breakpoint at line 16 by left clicking in the margin indicator bar (Fig. C.16).

Figure C.16. Setting a breakpoint in the program.


2.

Starting the debugger. Select Debug > Start Debugging. Enter the value 25 at the Enter withdrawal amount for account1: prompt. Program execution halts when the program reaches the breakpoint at line 16.

3.

Using the Step Into command. The Step Into command executes the next statement in the program (the yellow highlighted line of Fig. C.17) and immediately halts. If the statement to execute is a method call, control transfers to the called method. The Step Into command allows you to follow execution into a method and confirm its execution by individually executing each statement inside the method. Select Debug > Step Into (or press F11) to enter the Debit method (Fig. C.18).



Figure C.17. The Step Into command will enter the Debit method.


Figure C.18. Stepping into the Debit method.


4.

Using the Step Over command. Select Debug > Step Over to enter the Debit method's body and transfer control to line 23 (Fig. C.19). The Step Over command behaves like the Step Into command when the next statement to execute does not contain a method call or access a property. You will see how the Step Over command differs from the Step Into command in Step 10.

Figure C.19. Stepping over a statement in the Credit method.


5.

Using the Step Out command. Select Debug > Step Out to execute the remaining statements in the method and return control to the calling method. Often, in lengthy methods, you will want to look at a few key lines of code, then continue debugging the caller's code. The Step Out command executes the remainder of a method and returns to the caller.

6.

Setting a breakpoint. Set a breakpoint (Fig. C.20) at line 20 of Fig. C.2. You will make use of this breakpoint in the next step.

Figure C.20. Setting a second breakpoint in the program.


7.

Using the Continue command. Select Debug > Continue to execute until the next breakpoint is reached at line 20. This feature saves time when you do not want to step line-by-line through many lines of code to reach the next breakpoint.

8.

Stopping the debugger. Select Debug > Stop Debugging to end the debugging session.

9.

Starting the debugger. Before we can demonstrate the next debugger feature, you must restart the debugger. Start it, as you did in Step 2, and enter the same value(25). The debugger pauses execution at line 16.

10.

Using the Step Over command. Select Debug > Step Over (Fig. C.21). Recall that this command behaves like the Step Into command when the next statement to execute does not contain a method call. If the next statement to execute contains a method call, the called method executes in its entirety (without pausing execution at any statement inside the methodunless there is a breakpoint in the method), and the arrow advances to the next executable line (after the method call) in the current method. In this case, the debugger executes line 16 in Main (Fig. C.2), which calls the Debit method. Then, the debugger pauses execution at line 19, the next executable statement.

Figure C.21. Using the debugger's Step Over command.


11.

Stopping the debugger. Select Debug > Stop Debugging. Remove all remaining breakpoints.



Visual BasicR 2005 for Programmers. DeitelR Developer Series
Visual Basic 2005 for Programmers (2nd Edition)
ISBN: 013225140X
EAN: 2147483647
Year: 2004
Pages: 435

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net