Visual Basic Concepts

Printing Information in the Immediate Window

There are two ways to print to the Immediate window:

  • Include Debug.Print statements in the application code.

  • Enter Print methods directly in the Immediate window.

These printing techniques offer several advantages over watch expressions:

  • You don't have to break execution to get feedback on how the application is performing. You can see data or other messages displayed as you run the application.

  • Feedback is displayed in a separate area (the Immediate window), so it does not interfere with output that a user sees.

  • Because you can save this code as part of the form, you don't have to redefine these statements the next time you work on the application.

Printing from Application Code

The Print method sends output to the Immediate window whenever you include the Debug object prefix:

Debug.Print [items][;]

For example, the following statement prints the value of Salary to the Immediate window every time it is executed:

Debug.Print "Salary = "; Salary

This technique works best when there is a particular place in your application code at which the variable (in this case, Salary) is known to change. For example, you might put the previous statement in a loop that repeatedly alters Salary.

*Note* When you compile your application into an .exe file, Debug.Print statements are removed. Thus, if your application only uses Debug.Print statements with strings or simple variable types as arguments, it will not have any Debug.Print statements. However, Visual Basic will not strip out function calls appearing as arguments to Debug.Print. Thus, any side-effects of those functions will continue to happen in a compiled .exe file, even though the function results are not printed.

*For More Information* See "Debug Object."

Printing from Within the Immediate Window

Once you're in break mode, you can move the focus to the Immediate window to examine data.

To examine data in the Immediate window

  1. Click the Immediate window (if visible).


    From the View menu, choose Immediate Window.

    Once you have moved focus to the Immediate window, you then can use the Print method without the Debug object.

  2. Type or paste a statement into the Immediate window, and then press ENTER.

    The Immediate window responds by carrying out the statement, as shown in Figure 13.19.

    Figure 13.19 Using the Print method to print to the Immediate window

A question mark (?) is useful shorthand for the Print method. The question mark means the same as Print, and can be used in any context where Print is used. For example, the statements in Figure 13.19 could be entered as shown in Figure 13.20.

Figure 13.20 Using a question mark instead of the Print method

Printing Values of Properties

You can evaluate any valid expression in the Immediate window, including expressions involving properties. The currently active form or module determines the scope. If the execution halts within code that is attached to a form or class, you can refer to the properties of that form (or one of its controls) and make the reference to the form implicit with statements like the following:

? BackColor
? Text1.Height

Assuming that Text1 is a control on the currently active form, the first statement prints the numeric value of the current form's background color to the Immediate window. The second statement prints the height of Text1.

If execution is suspended in a module or another form, you must explicitly specify the form name as follows:

? Form1.BackColor
? Form1.Text1.Height

*Note* Referencing an unloaded form in the Immediate window (or anywhere else) loads that form.

*For More Information* To learn about changing properties and values in the Immediate window, see "Assigning Values to Variables and Properties" later in this chapter.