Sams Teach Yourself Visual Basic 6 in 24 Hours

Sams Teach Yourself Visual Basic 6 in 24 Hours

By Greg Perry

The Immediate Window

At any breakpoint you can select View | Immediate Window (Ctrl+G) to request the Immediate window (sometimes called the Debug window). The Immediate window is a special window in which you can directly type Visual Basic commands and view and change variables and control values during a program's execution.

For displaying variables and controls, apply the Print method (see Hour 16, "Printing with Visual Basic" ) to view variables and controls. When you use Print in the Immediate window, Visual Basic sends the output to the Immediate window and not to the Printer object, as you saw in Hour 16. For example, suppose that you set a breakpoint during a variable's assignment, as described in the previous sections, and you pressed Ctrl+G to open the Immediate window. The Immediate window recognizes simple Visual Basic commands and methods such as Print and assignment statements.

Figure 20.11 shows what happens if you print the value of strComputer after the variable is assigned the string value. Unlike the Quick Watch dialog box, the Immediate window has room to display multiple lines if you display a multiline control such as a text box. You can resize and move the Immediate window. Although they must use the Print command instead of simply clicking a variable or control, many programmers prefer to display values from the Immediate window instead of the Quick Watch dialog box. The Immediate window displays the entire value and contains a vertical scrollbar so that you can scroll through the values printed in the window.

20fig11.gif

Figure 20.11 The Immediate window displays the values of variables.

The Immediate window's scrolling and resizing features are so handy that some Visual Basic programmers prefer to send messages to the Immediate window at runtime rather than use the Quick Watch dialog box. For example, if you want to see the value of certain arguments when called procedures execute, you can add the Print methods at the top of those procedures that send the argument values to the Immediate window automatically as the program executes. When you get the bugs out of the program, you can remove the Print commands so that the Immediate window stays closed.

To print to the Immediate window, preface the Print method with the special Debug object. The following command, executed anywhere from an application's code, prints the values of two variables with appropriate titles in the Immediate window:

Debug.Print "Age:"; intAgeVal, "Weight:"; intWeightVal

All the Print method's options, including semicolons, commas, and Tab() and Spc() functions, work inside the Immediate window just as they do for the Printer object described in Hour 16. Be careful to specify the Debug object before the Print method, however. If you omit Debug, Visual Basic prints the output directly on the form itself!

The Immediate window recognizes assignments that you make to variables and controls. For example, suppose you know that a certain variable wasn't initialized properly earlier in the execution, but you still want to finish the program's execution as if the variable had its proper value. If you need to, you can assign that variable a new value directly within the Immediate window using the assignment statement. When you resume the program's execution, either in single-step or in runtime mode, the variable, from that point in the program, will contain the value that you assigned to it.

Share ThisShare This

Informit Network