How to Test and Debug an ASP.NET Application—How to use the Autos, Locals, and Watch windows to monitor variables
|Visual C# Tutorials|
© 2006 Mike Murach & Assoc.
How to use the Autos, Locals, and Watch windows to monitor variables
If you need to see the values of several application variables or properties, you can do that using the Autos, Locals, or Watch windows. By default, these windows are displayed in the lower left corner of the IDE when an application enters break mode. If they’re not displayed, you can display them by selecting the appropriate command from the Debug->Windows menu. Note that you can display up to four separate Watch windows.
The content of the Autos, Locals, and Watch windows is illustrated in figure 4-13. The difference between the Autos and Locals windows is in the amount of information they display and the scope of that information.
The Locals window displays information about the variables and controls within the scope of the current method. Since that includes information about the form and all of the controls on the form if the code in a form is currently executing, that information can be extensive.
In contrast, the Autos window displays information about the variables, properties, and constants used in the current statement, the three statements before that statement, and the three statements after that statement. Although the information in this window is more limited than the information shown in the Locals window, the Autos window helps you focus on the variables that are relevant to the current statement.
Unlike the Autos and Locals windows, the Watch windows let you choose the values that are displayed. The Watch window shown in this figure, for example, displays the Text property of the txtQuantity and lblUnitPrice controls, the Quantity property of the item object, and the UnitPrice property of the Product object that’s stored in the item object. The Watch windows also let you watch the values of expressions you specify. Note that an expression doesn’t have to exist in the application for you to add it to a Watch window.
To add an item to a Watch window, you can type it directly into the Name column. Alternatively, if the item appears in the Code Editor window, you can highlight it in that window and then drag it to a Watch window. You can also highlight the item in the Code Editor or a data tip and then right-click on it and select the Add Watch command to add it to the Watch window that’s currently displayed.
Besides displaying the values of variables and properties, you can use the Autos, Locals, and Watch windows to change these values. To do that, you simply double-click on the value you want to change and enter a new value. Then, you can continue debugging or continue the execution of the application.
Figure 4-13 How to use the Autos, Locals, and Watch windows to monitor variables
• The Autos window displays information about variables, properties, and constants in the current statement and the three statements before and after the current statement.
• The Locals window displays information about the variables and controls within the scope of the current method.
• The Watch windows let you view the values of variables and expressions you specify, called watch expressions. You can display up to four Watch windows.
• To add a watch expression, type a variable name or expression into the Name column, highlight a variable or expression in the Code Editor window and drag it to the Watch window, or right-click on a variable or highlighted expression in the Code Editor window or a data tip and choose Add Watch.
• To delete a row from a Watch window, right-click the row and choose Delete Watch. To delete all the rows in a Watch window, right-click the window and choose Select All to select the rows, then right-click and choose Delete Watch.
• To display any of these windows, click on its tab if it’s visible or select the appropriate command from the Debug->Windows menu.
• To change the value of a property or variable from any of these windows, double-click on the value in the Value column, then type a new value and press the Enter key.