Examining Values: The Autos and Locals Windows

Look at the tabs below the code window—if you’re using Visual Studio, you’ll find a Locals window and an Autos window, possibly as tabs in a single window along with some others. If you’re using C# Express, the Autos window won’t be there, but Locals will be. Both of these display your local variables. The difference is that the Autos window shows variables used in the current statement and the previous statement. (The current statement is the statement at the current execution location, which is highlighted automatically in the debugger—thus, the window’s name.) The Locals window displays all the variables in the current method, including parameters, as shown in Figure 9-8, along with each variable’s current value and type.

In Visual Studio, the Breakpoint menu allows you to select various options for your breakpoint settings.

Figure 9-5. In Visual Studio, the Breakpoint menu allows you to select various options for your breakpoint settings.

The Breakpoint Hit Count dialog lets you choose how often you want this point to be reached before breaking.

Figure 9-6. The Breakpoint Hit Count dialog lets you choose how often you want this point to be reached before breaking.

Tip

The debugger stacks the Autos and Locals windows together with other tabs, as shown in Figure 9-8. You are free to separate these windows or to move them to be tabbed with other windows. You can simply drag-and-drop the windows where you want them. When you drop one window onto another, the two windows are tabbed together.

The Breakpoint Condition dialog is more open-ended, offering greater flexibility in your breakpoints.

Figure 9-7. The Breakpoint Condition dialog is more open-ended, offering greater flexibility in your breakpoints.

The Locals window shows the values of the variables in the method you’re currently in.

Figure 9-8. The Locals window shows the values of the variables in the method you’re currently in.

To see how this works, put a breakpoint on the first line of the Run( ) method and run the program. When the program stops, press F10 to step over the creation of the new Box object. The Autos window shows you that the new Box has been created, as shown in Figure 9-9.

Because box1 has just been created, it is shown in red. Notice the plus sign (+) next to box1. box1 is a member of the Box class, as you know, and that class has several members. Clicking the plus sign reveals the internal state of this object, as shown in Figure 9-10.

Press F11 a few more times, and you’ll see that the Autos window changes to show you the new values, appropriate to the current line of code.

As mentioned earlier, the Locals window lets you look at all the variables in the current method simultaneously. If you look at the Locals window while the execution is in the Box constructor, you’ll see the local variables theLength, theWidth, and theHeight (the parameters to the constructor) and this (the current object). Expand the this variable, and you’ll see the Box object, with its members uninitialized. Press F11 to progress through the assignment of values to the member variables of the Box class. As you press the F11 key, the update is reflected in the Locals window, as shown in Figure 9-11.

The Autos window is similar to the Locals window, but shows values from the current statement.

Figure 9-9. The Autos window is similar to the Locals window, but shows values from the current statement.

Expanding the variable gives you a look at the state of the object.

Figure 9-10. Expanding the variable gives you a look at the state of the object.

If you watch the Locals window as you step through the application, you can see the assignment of the object’s member fields.

Figure 9-11. If you watch the Locals window as you step through the application, you can see the assignment of the object’s member fields.

Explore the Locals and Autos windows as you step through the program. When you want to stop, choose the Stop Debugging item from the Debug menu to stop processing and return to the editor.

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

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