Debugging Using the Watch Window
The Watch window provides a method for you to watch variables and expressions easily while the code is executing this can be invaluable when you are trying to debug unwanted results in a variable. You can even change the values of variables in the Watch window. You can also add as many variables and expressions as needed to debug your program. This provides a mechanism that allows you to watch the values of your variables change without any intervention on your part.
You can add and delete a variable or expression to the QuickWatch dialog box only when your program is in break mode. Therefore, before you run your program, you need to set a breakpoint before the variable or expression that you want to watch. Once the breakpoint has been reached, you can add as many Watch variables or expressions as needed.
In the following Exercise, you add the intIndex variable to the Watch window and also add an expression using the intIndex variable. This enables you to observe this variable and expression as you step through your code.
Exercise - Using QuickWatch
1. Start your program again. When your form displays, switch to the IDE and clear the current breakpoint by deleting it in the Breakpoints window or by clicking it in the gray margin where it is set. Then set a new breakpoint as shown:
2. You can only add a QuickWatch variable or expression while your program is paused. Click the Start button on the form so the breakpoint will be encountered and your program paused.
3. Once the breakpoint has been encountered, right-click the variable, intIndex, in the For . . . Next loop and choose QuickWatch from the context menu to invoke the QuickWatch dialog box. Notice that this variable has not only been added to the Expression drop-down box but has also been placed in the Current value grid in the dialog, as shown in below Figure. Click the Add Watch button to add this variable to the Watch window.
4. While you have the QuickWatch dialog box open, set an expression to be evaluated. Add the expression intIndex = 1 in the Expression drop-down box. Then click the Add Watch button to have this expression added to the Watch window. Now close the QuickWatch dialog box by clicking the Close button.
5. If you do not see the Watch window at the bottom of the IDE, select Debug Windows Watch Watch 1. You should now see a variable and an expression in the Watch window as shown here:
The second watch expression that you added here will return a value of True when the intIndex variable equals 1, so Visual Studio 2005 will set the type to Boolean once you enter the For . . . Next loop.
6. Step through your code line by line so that you can watch the value of the variable and expression change. Click the Step Into icon on the Debug toolbar to step to the next line of code. Keep clicking the Step Into icon to see the values of the variable and expression in the Watch window change.
7. When you are done, click the Continue icon on the Debug toolbar to let your code finish executing. Then stop your project once the form has been displayed.
click next Page - Debugging with the Locals Window. |