The Immediate window is used at design time to debug and evaluate expressions, execute statements, print variable values, and so forth. It allows you to enter expressions to be evaluated or executed by the development language during debugging. To display the Immediate window, open a project for editing, then choose Windows from the Debug menu and select Immediate.
You can use this window to issue individual Visual Studio commands. The available commands include EvaluateStatement, which can be used to assign values to variables. The Immediate window also supports IntelliSense.
This window can be particularly useful while debugging an application. For example, to check the value of a variable varA, you can use the Print Command:
The question mark (?) is an alias for Debug.Print, so this command can also be written:
Both versions of this command will return the value of the variable varA.
To issue a Visual Studio command in the Immediate window, you must preface the command with a greater than sign (>). To enter multiple commands, switch to the Command window.
You can use the Immediate window to execute a function or subroutine at design time.
To execute a function at design time
Copy the following code into a Visual Basic console application:
Module Module1 Sub Main() MyFunction(5) End Sub Function MyFunction(ByVal input as Integer) As Integer Return input * 2 End Function End Module
On the Debug menu, click Windows, and then click Immediate.
Type ?MyFunction(2) in the Immediate window and press Enter.
The Immediate window will run MyFunction and display 4.
If the function or subroutine contains a breakpoint, Visual Studio will break execution at the appropriate point. You can then use the debugger windows to examine your program state. For more information see Walkthrough: Debugging at Design Time and Breaking During Function Evaluation in the Visual Studio Debugger.
You cannot use design time expression evaluation in project types that require starting up an execution environment, including Visual Studio Tools for Office projects, Web projects, Smart Device projects, and SQL projects.
Design Time Expression Evaluation in Multi-Project Solutions
When establishing the context for design time expression evaluation, Visual Studio references the currently selected project in Solution Explorer. If no project is selected in Solution Explorer, Visual Studio attempts to evaluate the function against the startup project. If the function cannot be evaluated in the current context, you will receive an error message. If you are attempting to evaluate a function in a project that is not the startup project for the solution and you receive an error, try selecting the project in Solution Explorer and attempt the evaluation again.
You must enter the greater than sign (>) when issuing Visual Studio commands in the Immediate window. Use the UP ARROW and DOWN ARROW keys to scroll through previously issued commands.
Evaluate an expression.
Preface the expression with a question mark (?).
Temporarily enter Command mode while in Immediate mode (to execute a single command).
Enter the command, prefacing it with a greater than sign (>).
Switch to the Command window.
Enter cmd into the window, prefacing it with a greater than sign (>).
Switch back to the Immediate window.
Enter immed into the window without the greater than sign (>).
The window used to enter the EvaluateStatement command determines whether an equals sign (=) is interpreted as a comparison operator or as an assignment operator.
In the Immediate window, an equals sign (=) is interpreted as an assignment operator. So, for example, the command
will assign to variable varA the value of variable varB.
In the Command window, by contrast, an equals sign (=) is interpreted as a comparison operator. You cannot use assignment operations in the Command window. So, for example, if the values of variables varA and varB are different, then the command
will return a value of False.
In some settings configurations, first-chance exception notifications are displayed in the Immediate window.
To toggle first-chance exception notifications in the Immediate window
On the View menu, click Other Windows, and click Output.
Right-click on the text area of the Output window, and select or deselect Exception Messages.