Breakpoints and Data Tips

Breakpoints enable you to control the execution flow of your application. Basically a breakpoint breaks the execution of the application at the point where the breakpoint itself is placed so that you can take required actions (situation known as break mode). You can then resume the application execution. To place a breakpoint on a specific line of code, just place the cursor on the line of code you want to debug and then press F9.

Tip

To add a breakpoint, you can also right-click the line of code you want to debug and select the Breakpoint, Insert breakpoint command from the pop-up menu or just click the leftmost column in the code window.

A breakpoint is easily recognizable, because it highlights in red the selected line of code (see Figure 2.29).

Figure 2.29 Placing a breakpoint in the code editor.

image

To see how breakpoints work, we can run the sample application by pressing F5. When the debugger encounters a breakpoint, it breaks the execution and highlights in yellow the line of code that is being debugged, as shown in Figure 2.30, before the code is executed.

Figure 2.30 When encountering a breakpoint, Visual Studio highlights the line of code that is currently debugged.

image

If you take a look at Figure 2.31, you notice a couple of amazing things. First, if you pass with the mouse pointer over the message variable, IntelliSense shows the content of the variable itself, which at the moment contains no value (in fact is set to Nothing). This feature is known as Data Tips and is useful if you need to know the content of a variable or of another object in a particular moment of the application execution. Another interesting feature is that the Call Stack window shows that a breakpoint is currently available and encountered on the specified line of code. The Call Stack window is discussed in Chapter 5.

Figure 2.31 Using the Step Into command enables us to check if the variable has been assigned correctly.

image

The Visual Studio Historical Debugger

If you run the Microsoft Visual Studio 2010 Ultimate edition, you notice also another window called Debug History that is a new feature of Visual Studio 2010. This window is known as the Visual Studio Historical Debugger and is specific to the Visual Studio Team System instrumentation and will be discussed in Chapter 57, “Introducing the Visual Studio Extensibility.”

You can then execute just one line of code at a time, by pressing F11. For example, supposing we want to check if the message variable is correctly initialized at runtime, we could press F11 (which is a shortcut for the Step Into command in the Debug menu). The line of code where the breakpoint is placed will now be executed, and Visual Studio will highlight the next line of code. At that point you can still pass the mouse pointer over the variable to see the assignment result, as shown in Figure 2.31.

When you finish checking the assignments, you can resume the execution by simply pressing F5. At this point the execution of the application continues until another breakpoint or a runtime error is encountered. We discuss this second scenario next.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.62.160