If there’s a single feature of Visual Studio that every developer uses and is essential to the development process it is the built-in debugger.




Debugging can be commenced by clicking the green arrow button in the VS toolbar, selecting Debug-Strart Debugging from the menu, or hitting F5. Before commencing debugging you will need to select what exactly you are debugging, just starting debugging will commence the project from the default point. For a Web Forms app, if you want to start from another page other the default start page right click the page and select Set As Start Page. For a Win Forms app set the start Form in the application properties window first.



Prior to debugging, it will almost always be necessary to to set some breakpoints. Breakpoints pause the execution of the code and allow developers to examine controls and variables before allowing the program to continue to execute. Set a breakpoint by clicking in the margin of the code editor to and a red ball with code highlighted will appear (as shown below). The program’s execution will now be halted immediately before the highlighted line of code is hit.



ScreenHunter 01 Feb. 11 15.31 Visual Studio Debugging Tutorial Basics



So if we now hit F5 and start debugging there should be a delay as the project is built and the code will then be highlighted in yellow with a yellow arrow at the left:

ScreenHunter 02 Feb. 11 15.42 Visual Studio Debugging Tutorial Basics



At this point we can view variable values by hovering over them. Hovering over the vsTutor variable shows a value of 0 , hovering over the i variable does not work as the highlighted line as not been executed and the i variable not instantiated.



There are a number of important windows to monitor objects as the code executes, these are located in the bottom left by default but if not visible they can be opened from Debug>Windows.

The Watch window allow monitoring of any object which can simply be highlighted in the code window and dragged to the Watch window. The Watch window monitor objects regardless of whether they are in scope or now.

The Locals window cannot have objects dragged into it and shows all objects that are currently in scope. A powerful feature of the locals window is that it allow the objects to by modified. In this example, the value of the vsTutor variable can be modified by clicking and changing the number in the Value column.

The Autos window shows the objects used in the execution of the current statement.

The Immediate window allows for statements to be written and executed as the program’s execution is paused.

The Breakpoints window allows the breakpoints for the project to be managed by enabling, disabling or deleting them.



At all times the yellow arrow shows the line of code that will be executed next. This arrow can be dragged down to execute subsequent lines of code without executing the code that it is dragged over.



When in debug mode the Debug toolbar will either appear in the main tool bar of Visual Studio or a floating toolbar as show below.



ScreenHunter 04 Feb. 11 16.09 Visual Studio Debugging Tutorial Basics



The three buttons to the right of the yellow arrow are crucial to the debug process:

Step Into executes the next line of code that the program would normally execute.

Step Over proceeds to the next line of code in the current procedure, this means that other routines (such as functions) are called it will not proceed into those routines but simply execute them and continue to the next line of the current code block.

Step Out will move to the line of code which called the current process or the next breakpoint if that comes first.

In the next Debugging Tutorial we will proceed to examine more advanced Visual Studio debugging topics.