• No results found

Using the Debugger

In document Introduction to Visual Studio 2010 (Page 31-36)

In the procedure division, add the following code: 

  The point of this exercise is just to give us some code that we can interact with in the debugger later. 

Using the Debugger

In Visual Studio the debugger is completely embedded within the development environment, making it  much easier to step through code and examine data. The debugger has many features, so we’ll just take  a brief look at some of the more fundamental things that you can do. 

Breakpoints work in much the same way that you would expect in traditional Synergy, but are easier to  work with as a result of the graphical nature of the way that they are set, cleared and visualized. 

Code editor windows all have a small region at the left margin where breakpoints can be set. To set a  breakpoint at a certain position you simply click in this region, and you will see a visual representation  of the breakpoint, like this: 

The red circle indicator in the breakpoint region indicates that a breakpoint is set at that position. If you  execute the application in debug mode then execution will be interrupted at that point. 

It is also possible to have breakpoints set in your project, but disable one or more of those breakpoints  so that they will not fire while you are debugging. To disable a breakpoint right‐click on the breakpoint  indicator and select Disable Breakpoint. Disabled breakpoints look like this. 

  Re‐enable a disabled breakpoint by right‐clicking the breakpoint indicator and selecting Enable 

Breakpoint. You can also toggle between an enabled and disabled state by placing your cursor on the  source line and pressing Ctrl + F9. 

It is also possible to disable or enable all breakpoints via items on the Debug menu. 

If you wish to remove the breakpoint, simply click on the breakpoint indicator, or right‐click it and select  Delete Breakpoint. 

In the constructor method, Form1, add a breakpoint on the statement that calls the  InitializeComponent method, like this: 

   

Also add a breakpoint on the button1_Click method, like this: 

 

There is also an easy way to examine all of your current breakpoints using a debugger tool window. 

From the menu, select Debugger > Windows > Breakpoints, or press Ctrl + Alt + B. 

  You should see the Breakpoints window which lists all of the breakpoints that you currently have set. 

Double‐clicking on a breakpoint in the list will take place your cursor on the associated line of code, and  you can also easily add, remove, enable and disable breakpoints through this window. 

Execute the application by pressing F5 (Start Debugging). As an alternative, you can also use  the Start Debugging toolbar button, which looks like this  . 

When you start debugging you may notice that the layout of the Visual Studio user interface changes. 

Visual studio maintains different window layouts when in debugging mode, and by default shows you  several windows which are useful during the debugging process. You can customize the window layouts  like we did earlier. 

You won’t see the application on the screen yet, because we set a breakpoint in the constructor  method, and the form has not yet been initialized. What you should see is something like this: 

 

The yellow arrow icon over the breakpoint indicator and the yellow highlight on the code indicates that  a breakpoint was encountered, and Visual Studio has handed control back to you. From this point on  you can determine what happens, using the same sort of techniques that you would if you were  debugging in traditional Synergy. 

You can control the flow of execution using commands like Step Into (F11), Step Over (F10), Step Out  (Shift+F11) and Continue (F5). These options are available on the Debug menu column, and also have  corresponding icons on the Debug toolbar, which shows up when you start the debugger. It looks like  this: 

  

Step INTO the InitializeComponent() method by pressing F11, or by clicking the   toolbar  button. 

Start to step through the method by pressing F10 a few times, or by clicking the   toolbar  button. 

Step out of the method by pressing Shift+F11, or by clicking the   toolbar button. 

Continue execution by pressing F5, or by clicking the   toolbar button. 

You should now see the application appear on the screen, and it should operate normally until a 

breakpoint is encountered. You set a breakpoint on the event handler for the button’s Click method, so: 

Click the button on the form. 

The debugger should now break on the proc statement of the button1_Click method. In the lower part  of the screen you should see a window called Locals; you may need to click on its tab to bring it to the  front. 

If you can’t see the locals window, select Debug > Windows > Locals from the menu. 

The locals window is very useful, it displays information about the local variables in the current scope. In  this case, the button1_Click method. You can use the window to view the content of local variables, and  you can also change the value of the local variables, at least for value types. 

Press the F10 key to step into the procedure division. 

Double‐click in the Value cell for the var3 local variable, then type a new numeric value and  press enter. 

In the locals window, click on one of the other local variables. 

You will see that when the value of a variable changes, the locals window temporarily displays the value  in red to indicate that it recently changed. 

Press the F10 key again to execute the first assignment statement and step to the next  statement. 

You will notice that the variable that you previously changed is no longer displayed in red, but that the  var1 variable is now in red, because the statement that was just executed changed the value of the  variable. 

Press F10 again to execute the second assignment statement. 

Press F10 again to execute the addition expression and calculation. 

The yellow arrow indicator (next statement to execute) should now be pointing to the Debug.Print  statement which can be used to add tracing information to the output window in Visual Studio. This can  also be a useful debugging technique. If the application is executed normally (without debugging) then  this statement won’t do anything. 

Make sure that the output window is visible. 

Press F10 to execute the Debug.Print statement 

You should see the text var1 + var2 = 3 printed in the output window. 

Another useful window is the Immediate Window, which should be one of the tabs towards the bottom  of the screen. 

If you can’t see the Immediate Window then make it visible by selecting Debug > Windows > 

Immediate from the menu, or by pressing Ctrl + Alt + I. 

Check that the Immediate window is shown by clicking on its tab. 

The Immediate window allows you to type an expression into the window and have Visual Studio  evaluate the expression and display the resulting value. 

Click in the Immediate Window and type ?var1 and press enter. 

The debugger should display the value of the expression var1, in this case the value 1. 

In the Immediate Window, type ?var3 – var1 and press enter. 

Again, the debugger should display the value of the expression, 2. 

Another way to interact with data is via the Watch window, which may not be displayed by default. This  window allows you to define expressions that you are interested in looking at on an ongoing basis. 

These expressions could be simple variable names, or more complex expressions. 

Stop the program by selecting Debug > Stop Debugging, or by pressing Shift + F5. 

Press Ctrl + Shift + F9 and click the Yes button to remove all breakpoints. 

Add a new breakpoint on the var1 = 1 statement. 

Press F5 to start a new debugging session. 

Once your application starts, in Visual Studio display the Watch window by selecting Debug > 

Windows > Watch > Watch 1 from the menu. 

Go back to your application and click the button. 

In the Watch 1 window, click in the empty cell below the Name column, type var1 and press  enter. 

Repeat this, but this time for the expression var2. 

Add another watch, this time for the expression var1+var2. 

Your Watch window should now look something like this: 

 

Press F10 to step into the procedure division, then continue pressing F10 to step through the  assignment statements and notice what happens in the Watch window. 

Defining expressions in the Watch window can be a very useful technique when debugging complex  problems. 

Press Shift + F5 to stop the debugger. 

Press Ctrl + Shift + F9 and click the Yes button to remove all breakpoints. 

The Visual Studio debugging environment is very powerful, and this has only been a VERY brief  introduction to its most basic features. 

 

In document Introduction to Visual Studio 2010 (Page 31-36)

Related documents