Tracking Variables by Using a Watch Window


Tracking Variables by Using a Watch Window

The Autos window is useful for examining the state of certain variables and properties as they're evaluated by the compiler, but items in the Autos window persist, or maintain their values, only for the current statement (the statement highlighted in the debugger) and the previous statement (the statement just executed). When your program goes on to execute code that doesn't use the variables, they disappear from the Autos window.

To view the contents of variables and properties throughout the execution of a program, you need to use a Watch window, a special Visual Studio tool that tracks important values for you as long as you're working in debugging mode. In Visual Basic 6, you can open one Watch window to examine variables as they change. In Visual Studio, you can open up to four Watch windows, numbered Watch 1, Watch 2, Watch 3, and Watch 4. You can open these windows, by pointing to the Windows command on the Debug menu, pointing to Watch, and then clicking the window you want on the Watch submenu. You can also add expressions, such as Age >= 13, to a Watch window.

Open a Watch window

TIP
The Debug Test project is located in the c:\vb05sbs\chap08\debug test folder.

  1. Click the Start Debugging button on the Standard toolbar to run the Debug Test program again.

    I'm assuming that the breakpoint you set on the line Age = TextBox1.Text in the previous exercise is still present. If that breakpoint isn't set, stop the program now, and set the breakpoint by clicking in the Margin Indicator bar next to the statement, as shown in step 10 of the previous exercise, and then start the program again.

  2. Type 20 in the Age text box, and then click Test.

    The program stops at the breakpoint, and Visual Studio enters debugging mode, which is where you need to be if you want to add variables, properties, or expressions to a Watch window. One way to add an item is to select its value in the Code Editor, right click the selection, and then click the Add Watch command.

  3. Select the Age variable, right click it, and then click the Add Watch command.

    Visual Studio opens the Watch 1 window and adds the Age variable to it. The value for the variable is currently 0, and the Type column in the window identifies the Age variable as an Integer type.

    Another way to add an item is to drag the item from the Code Editor into the Watch window.

  4. Select the TextBox2.Text property, and drag it to the empty row in the Watch 1 window.

    When you release the mouse button, Visual Studio adds the property and displays its value. (Right now, the property is an empty string.)

  5. Select the expression Age < 20, and add it to the Watch window.

    Age < 20 is a conditional expression, and you can use the Watch window to display its logical, or Boolean, value, much as you did by holding the pointer over a condition earlier in this chapter. Your Watch window looks like this:

    graphic

    Now step through the program code to see how the values in the Watch 1 window change.

  6. Click the Step Into button on the Debug toolbar.

    TIP
    Instead of clicking the Step Into button on the Debug toolbar, you can press the F11 key on the keyboard.

    The Age variable is set to 20, and the Age < 20 condition evaluates to False. These values are displayed in red type in the Watch window because they've just been updated.

  7. Click the Step Into button three more times.

    The Else clause is executed in the decision structure, and the value of the TextBox2.Text property in the Watch window changes to “You're not a teenager.” This conditional test is operating correctly. Because you're satisfied with this condition, you can remove the test from the Watch window.

  8. Click the Age < 20 row in the Watch window, and then press Delete.

    Visual Studio removes the value from the Watch window. As you can see, adding and removing values from the Watch window is a speedy process.

Leave Visual Studio running in debugging mode for now. You'll continue using the Watch window in the next section.



Microsoft Visual Basic 2005 Step by Step
Microsoft Visual Basic 2005 Step by Step (Step by Step (Microsoft))
ISBN: B003E7EV06
EAN: N/A
Year: 2003
Pages: 168

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net