Running Code in the Immediate Window
The Immediate window is one of my favorite tools. The Immediate window lets you try code outside of your program. You can test very simple or very complex code in the Immediate window. To open the Immediate window, click the View, Immediate Window menu item in the editor.
The Immediate window takes some getting used to because there are few limitations on the code you can write and test in the Immediate window (see Figure 3.8).
Figure 3.8 The Immediate window used to print the value of the constant PI.
To run code in the Immediate window, open the Immediate window, type code in the window, and press the Enter key. Try these steps to print the value of the constant PI from Listing 3.3:
Step through the code in Listing 3.3 until you get to line 5.
Click View, Immediate Window to open the Immediate window.
Click the Immediate window to select it.
Type ? PI in the Immediate window. (? is the abbreviated version of the print command; ? is a remnant from older versions of Basic. If you type ? in your code the Visual Basic Editor will expand ? to Print. For example, Debug.? Pi is converted to Debug.Print Pi.)
Press Enter.
The value assigned to PI, 3.14159, is shown in Figure 3.8. Listing 3.4 demonstrates that you can write complex code in the Immediate window.
Listing 3.4 Immediate Code That Opens and Reads a Text File from Your Computer
1: open "c:\winnt\win.ini" for input as #1 2: line input #1, s 3: print s 4: close #1
Line 1 opens the file c:\winnt\win.ini, which is on every Windows computer. Line 2 uses the Line Input command to read the first line of text from the opened file referred to by the handle #1. Line 3 prints the value read into the implicit variable. The output from line 3 on my PC running Windows 2000 is
; for 16-bit app support
Line 4 closes the file. The Immediate window is a great place to test your code ideas before implementing them in your program.