The Output window is where many of the tools, including the compiler, send their output. Every time you start an application, a series of messages is displayed in the Output window. These messages are generated by the compiler, and you need not understand them at this point. If the Output window is not visible, choose View > Other Windows > Output from the menu.
Command and Immediate Windows
While testing a program, you can interrupt its execution by inserting a so-called breakpoint. When the breakpoint is reached, the program’s execution is suspended, and you can execute a statement in the Immediate window. Any statement that can appear in your VB code can also be executed in the Immediate window. To evaluate an expression, enter a question mark followed by the expression you want to evaluate, as in the following samples, where result is a variable in the program you interrupted:
? "The answer is " & result.ToString
You can also send output to this window from within your code with the Debug.Write and Debug.WriteLine methods. Actually, this is a widely used debugging technique — to print the values of certain variables before entering a problematic area of the code. There are more elaborate tools to help you debug your application, and you’ll find a discussion in the section “Debugging and Error Handling in Visual Basic 2008”, but printing a few values to the Immediate window is a time-honored practice in programming with VB.
In many of the examples of this tutorial, especially in the first few chapters, I use the Debug.WriteLine statement to print something to the Immediate window. To demonstrate the use of the DateDiff() function, for example, I’ll use a statement like the following:
Debug.WriteLine(DateDiff(DateInterval.Day, #3/9/2007#, #5/15/2008#))
When this statement is executed, the value 433 will appear in the Immediate window. This statement demonstrates the syntax of the DateDiff() function, which returns the difference between the two dates in days. Sending some output to the Immediate window to test a function or display the results of intermediate calculations is a common practice.
To get an idea of the functionality of the Immediate window, switch back to your first sample application and insert the Stop statement after the End If statement in the button’s Click event handler. Run the application, select a language, and click the button on the form. After displaying a message box, the application will reach the Stop statement and its execution will be suspended.
You’ll see the Immediate window at the bottom of the IDE. If it’s not visible, open the Debugmenu and choose Windows > Immediate. In the Immediate window, enter the following statement:
Then press Enter to execute it. Notice that IntelliSense is present while you’re typing in the Immediate window. The expression prints the number of items in the ComboBox control. (Don’t worry about the numerous properties of the control and the way I present them here; they’re discussed in detail in Chapter, ‘‘Basic Windows Controls.’’) As soon as you press Enter, the value 5 will be printed on the following line.
You can also manipulate the controls on the form from within the Immediate window. Enter the following statement and press Enter to execute it:
ComboBox1.SelectedIndex = 4
The fifth item on the control will be selected (the indexing of the items begins with 0). However, you can’t see the effects of your changes, because the application isn’t running. Press F5 to resume the execution of the application and you will see that the item Cobol is now selected in the ComboBox control.
The Immediate window is available only while the application’s execution is suspended. To continue experimenting with it, click the button on the form to evaluate your choice. When the Stop statement is executed again, you’ll be switched to the Immediate window.
Unlike the Immediate window, the Command window is available at design time. The Command window allows you to access all the commands of Visual Studio by typing their names in this window. If you enter the string Edit followed by a period, you will see a list of all commands of the Edit menu, including the ones that are not visible at the time, and you can invoke any of these commands and pass arguments to them. For example, if you enter Edit.Find “Margin” in the Command window and then press Enter, the first instance of the string Margin will be located in the open code window. To start the application, you can type Debug.Start. You can add a new project to the current solution with the AddProj command, and so on. Most developers hardly ever use this window in designing or debugging applications.
Error List Window
This window is populated by the compiler with error messages, if the code can’t be successfully compiled. You can double-click an error message in this window, and the IDE will take you to the line with the statement in error — which you should fix. Change the MsgBox() function name to MsssgBox(). As soon as you leave the line with the error, the name of the function will be underlined with a wiggly red line and the following error description will appear in the Error List window:
Name ’MsssgBox’ is not declared