160Microsoft Visual Studio 2010: A Beginner’s Guide

Inspecting Application State

Application state is the value of variables in your code, the current path of execution, or any other information that tells you what your program is doing. While debugging, it’s important to be able to view application state and compare what is really happening to what you expected to happen. VS gives you various windows for viewing application state, which you’ll learn about in this section.

NOTE

When inspecting the state of your application, you’ll need to keep the concept of scope in mind. When a variable is in scope, you will be able to see the variable’s value. Scope is defined within a block. In C#, the block is defined with curly braces, and VB defines a block with begin and end statements. A couple examples of scope involve class fields and local variables. A private class field would be in scope for all the methods of that class but not in another class. A local variable would be in scope for all statements

of the method it is defined in, but would be out of scope for other methods. Another scenario is a for loop that defined a variable in its body—the variable would be in scope for the body of the loop but out of scope outside of the loop body.

Locals and Autos Windows

The Locals and Autos windows show you the variables in your system at the current breakpoint. Locals gives you a list of all variables that the current statement could access (also referred to as in scope). The Autos window shows variables from the current and previous lines. You can open the Locals and Autos windows from the Debug Windows menu when your VS debug session is active and paused at a breakpoint. These windows may have already been placed for you at the bottom left of Visual Studio next to the Output window if you’ve not rearranged your VS layout.

As shown in Figure 6-8, the Locals window shows all of the variables in scope for the Main method from Listing 6-1. The Locals window is a coarse-grained view, and the

Figure 6-8 The Locals window

Page 183
Image 183
Microsoft 9GD00001 manual Inspecting Application State, Locals and Autos Windows