BlackWaspTM
Debugging
VS 2003+

Visual Studio Debug Mode (2)

In addition to being a complex code editing tool, Visual Studio includes a debugger. This tool allows a program to be paused, then stepped through line-by-line whilst allowing you to monitor the values of variables, helping to identify bugs.

Stepping Through Code

Another option for running a program in debug mode is the Step Into command, which can be accessed using the F11 key. This starts the program and pauses immediately before the first statement in the code is executed. If you try running the program using the F11 key, you should see the yellow highlight appear on the first brace character of the Main method's code block.

The Step Into command can be used any time a program is paused by the debugger. Each press of the key causes a single command or line of code to be executed. If you press F11 again, the highlight will move to the line that declares the table variable. Continue pressing the key until you reach the call to the Multiply method. The next press of F11 steps into the method. Keep stepping through the code until you are happy with the manner in which the command works, then remove the breakpoint and press F5 to allow the program to run to completion.

Sometimes you will want to step through a program but without seeing the internal operations of a method or property. In our example, you may want to step through the loop but not work through the Multiply method. In this situation, you can use the Step Over command, which is accessed using the F10 key. You can see this in action by starting the program by pressing F11, then stepping through the code until the code calling Multiply is highlighted. Pressing F11 would step into the method, pressing F10 executes the method and moves to the line following the call.

Occasionally you will be stepping through a program and will step into a method or property. Once within the member, you can use the Step Out command to execute the rest of the method code and return to the calling function. The Step Out command is accessed by pressing Shift-F11. Try stepping into the Multiply method and pressing this key combination.

NB: When using a command that executes several lines of code, the program will pause again if a breakpoint is met.

Stopping and Restarting

Whilst using the debugger, you may want to cease execution of the program so that you can return to editing the code. This is achieved using the Stop Debugging command or toolbar button, or by pressing Shift-F5. You can also stop and restart the program by using the Restart command or pressing Ctrl-Shift-F5.

Controlling the Program Flow

Often you will create breakpoints to force the executing assembly to halt at a specific point in the code. However, occasionally you may want to run until a command is reached but will not want a breakpoint to be inserted. If you right-click the desired location, a context-sensitive menu will appear. Select "Run to Cursor" from this menu to run the program until the clicked position is reached. NB: If the position is never reached, the code may never halt.

You can also change the program flow to select the next command to execute. If you right-click a statement and select "Set Next Statement" from the menu that appears, the selected command will be highlighted as the next statement to process. In this case, any commands between the original position and the new location are ignored. You can even select a command that has already executed, moving backwards through the code.

23 October 2008