Using Immediate Window to Work with Values

    August 30, 2006

This article is an excerpt from the book: Murach’s ASP.NET 2.0 Web Programming with C# 2005.

The Immediate window, shown in figure 4-14, is useful for displaying the values of variables or properties that don’t appear in the Code Editor window. To display a value, you simply type a question mark followed by the name of the variable or property. The first line of code in this figure, for example, displays the Text property of the item selected from the Products drop-down list. You can see the result in the second line of this window.

The Immediate window is also useful for executing C# statements. For example, you can execute an assignment statement to change the value of a variable or property. After I displayed the Text property of the Quantity text box, for example, I assigned a value of 1 to this property. Similarly, you can execute a user-defined method. This can be useful for testing the result of a method with different arguments. If you execute a method that returns a value, you can also preface the method name with a question mark to display the value it returns.

When you enter commands in the Immediate window, they’re executed in the same context (or scope) as the application that’s running. That means that you can’t display the value of a variable that’s out of scope. If you try to do that, the debugger displays an error message.

The commands that you enter into the Immediate window remain there until you exit from Visual Studio or explicitly delete them using the Clear All command in the shortcut menu for the window. That way, you can use standard Windows techniques to edit and reuse the same commands from one execution of an application to another without having to reenter them.

To execute a command that you’ve already entered in the Immediate window, just use the Up and Down arrow keys to scroll through the commands. As you scroll, the commands are displayed at the bottom of the window. Then, you can change a command if necessary and press Enter to execute it.

The Immediate window


    You can use the Immediate window to display and assign values from a program during execution. To display this window, click on the Immediate Window tab or use the DebugWindowsImmediate command.

    To display a value in the Immediate window, enter a question mark followed by the expression whose value you want to display. Then, press the Enter key.

    To assign a different value to a variable, property, or object, enter an assignment statement in the Immediate window. Then, press the Enter key.

    To execute a user-defined method from the Immediate window, enter its name and any arguments it requires. Then, press the Enter key. If you want to display the value that’s returned by a method, precede the method call with a question mark.

    To reissue a command, use the Up and Down arrow keys to scroll through the commands until you find the one you want. Then, modify the command if necessary and press the Enter key to execute it.

    To remove all commands and output from the Immediate window, use the Clear All command in the shortcut menu for the window.

Add to | Digg | Yahoo! My Web | Furl

Bookmark WebProNews:

Joel Murach has been writing and editing for more than 10 years. During that time, he sharpened his programming skills as a contract programmer in San Francisco and his instructional skills as a trainer for HarperCollins Publishing. He always brings a vision to his projects that leads to improved effectiveness for his readers.