Understanding and using the debugging features of Visual Studio is an important part of developing applications quickly and efficiently.
Note: This discussion is based on Visual Studio 2008 – similar features exist in all versions of Visual Studio, but the exact names or menu locations may differ.
The important features provided for debugging are:
- Examine variable values when an error occurs or at a breakpoint
- Insert a breakpoint to stop program execution at any point
- Restart program execution, at the current line or at another line
- Change program code or variable values before restarting program execution
There are many other features available while debugging, but the above four items are the most important to master at the start.
1. Examine variable values. When the program stops in the debugger, the values of all current variables are available for inspection. Hover the mouse over a variable to see a window with the variable value. For complex objects there will be a plus sign that you can use to drop down a larger window with more detail.
Or, you can add a variable to the watch window. Right click on the variable name and choose Add Watch. The Watch window will open with the item added.
Or, you can open the Local Variables window (Debug \ Windows \ Locals) and see the current values of all variables that are local to the current procedure.
2. Stop the program at a predetermined point. In the code window, move the cursor to the lne where you want the program to stop, right click, and select Breakpoint \ Add Breakpoint. An large dot will appear in the margin. When the program is run, it will stop immediately before executing this line. At that point you can examine the variables (see above), make changes and continue, or terminate the program. Conditional breakpoints can also be used – they enable you to stop the program at a predetermined point, but only if a certain condition is met. To explore some of the conditional options for breakpoints, right-click on the large dot that appears in the margin when a breakpoint is created.
3. Continue the program. You can execute the line that the program is currently stopped at with Debug / Step Into. The line will be executed and the program will stop before executing the next line. Debug \ Step Over is similar, except that it will skip subroutines and functions. Debug \ Continue will continue the program until the next breakpoint or error. If the program is stopped at an error, the above commands will re-try the line that errored, unless the current line has been set to a different line (see next).
Or, right click on a different line and select Set Next Statement. This will set the selected line as the next statement to be executed. You can then execute the statement with Debug \ Step Over, Debug \ Step Into or Debug \ Continue. Set Next Statement, followed by step into or continue, can be very useful when you have made changes to variables, but you need to re-do some calculation that occurred prior to the current line. You can also use it when you have an error, but the program can continue if you simply exit the current routine – simply set the next statement to the End statement of the routine.
Or, stop the program with Debug \ Stop Debugging.
Note that program execution cannot restart at a line that is not part of the current scope. Roughly, this means that the line must be within the current routine, and must not be inside a lower-nested routine. You can set next statement to and End Sub to terminate a sub immediately on restart.
4. Change program code or variable values. You can make changes to code, and you can change the values of variables before re-starting program execution.
You can make code changes to any part of the program, in the same way that you edit the program when it is not running. However, some changes will be flagged as not available while debugging. This includes things like declaring some types of obects, adding new routines and changing event handlers. You will be warned that your edit is not compatible with debug mode and given the opportunity to undo the edit or terminate the program.
Note: Some syntax errors are flagged as a change that requires the program to be terminated. Check your code carefully before terminating, because often the program can be continued if you simply fix the error.
Variables are changed using the Watch window. Add the variable as a Watch variable (see 2, above) then double-click on the value, and type in a new value.