Testing Procedures with the Immediate Window

See Also

The Immediate window evaluates any valid Visual Basic executable statement, but it doesn't accept data declarations. You can enter calls to Sub and Function procedures, however, which allows you to test the possible effect of a procedure with any given set of arguments. Simply enter a statement in the Immediate window (while in break mode) as you would in the Code window. For example:

X = Quadratic(2, 8, 8)
DisplayGraph 50, Arr1
Form_MouseDown 1, 0, 100, 100

When you press the ENTER key, Visual Basic switches to run time to execute the statement, and then returns to break mode. At that point, you can see results and test any possible effects on variables or property values.

Scope applies to procedure calls just as it does to variables. You can call any procedure within the currently active form. You can always call a procedure in a module, unless you define the procedure as Private, in which case you can call the procedure only while executing in the module.

For More Information   Scope is discussed in "Introduction to Variables, Constants, and Data Types" in "Programming Fundamentals."

Viewing and Testing Multiple Instances of Procedures

You can use the Immediate window to run a procedure repeatedly, testing the effect of different conditions. Each separate call of the procedure is maintained as a separate instance by Visual Basic. This allows you to separately test variables and property settings in each instance of the procedure. To see how this works, open a new project and add the following code to the form module:

Private Sub Form_Click()
   AProcedure
End Sub

Sub AProcedure()
   Dim intX As Integer
   intX = 10
   BProcedure
End Sub

Sub BProcedure()
   Stop
End Sub

Run the application and click the form. The Stop statement puts Visual Basic into break mode and the Immediate window is displayed. Change the value of intX to 15 in the procedure "AProcedure," switch to the Immediate window, and type the following:

AProcedure

This calls the procedure "AProcedure" and restarts the application. If you switch to the Immediate window and run "AProcedure" again, and then open the Call Stack dialog box, you'll see a listing much like the one in Figure 13.21. Each separate run of the program is listed, separated by the [<Debug Window>] listing.

Figure 13.21   The Call Stack dialog box shows multiple instances of procedures

Visual Basic maintains a listing of the procedures executed by each command from the Immediate window. Newer listings are at the top of the list. You can use the Call Stack dialog box to select any instance of a procedure, and then print the values of variables from that procedure in the Immediate window.

For example, if you double-click the earliest instance of "AProcedure" and use the Immediate window to print the value of intX, it will return 10, as shown in Figure 13.22. If you changed the value of intX to 15 for the second run of the "AProcedure," that value is stored with the second instance of the procedure.

Figure 13.22   Printing the values of variables in the Immediate window

Note   Although most statements are supported in the Immediate window, a control structure is valid only if it can be completely expressed on one line of code; use colons to separate the statements that make up the control structure. The following For loop is valid in the Immediate window:

For I = 1 To 20 : Print 2 * I : Next I