Using the Immediate and Command Windows


Using the Immediate and Command Windows

So far, you've used the Visual Studio debugging tools that allow you to enter debugging mode, execute code one statement at a time, and examine the value of important variables, properties, and expressions in your program. Now you'll learn how to change the value of a variable by using the Immediate window, and you'll learn how to run commands, such as Save All or Print, within the Visual Studio IDE by using the Command window. (An improvement in Visual Studio 2005 is that these two windows are more distinct than they were in Microsoft Visual Basic .NET 2003.) The windows contain scroll bars, so you can execute more than one command and view the results by using the arrow keys.

The following exercises demonstrate how the Immediate and Command windows work. I discuss these windows together because, with the following special commands, you can switch between them:

  • In the Immediate window, the >cmd command switches to the Command window.

  • In the Command window, the immed command switches to the Immediate window.

The exercises assume that you're debugging the Debug Test program in debugging mode.

Use the Immediate window to modify a variable

  1. On the Debug menu, point to Windows, and then click Immediate.

    When you select the command, Visual Studio opens the Immediate window and prepares the compiler to receive commands from you while the Debug Test program is running. This is a very handy feature, because you can test program conditions on the fly, without stopping the program and inserting program statements in the Code Editor.

  2. In the Immediate window, type Age = 17, and then press Enter.

    You've just used the Immediate window to change the value of a variable. The value of the Age variable in the Watch window immediately changes to 17, and the next time the If statement is executed, the value in the TextBox2.Text property will change to “You're a teenager.” Your Immediate window looks like this:

    graphic

  3. Type the following statement in the Immediate window, and then press Enter:

    TextBox2.Text = "You're a great age!"

    The Text property of the TextBox2 object is immediately changed to “You're a great age!” In the Immediate window, you can change the value of properties, as well as variables.

  4. Display the Watch 1 window if it is not currently visible. (Click the Watch 1 tab in the Visual Studio IDE.)

    The Watch window looks like this:

    graphic

    As you can see, both items now contain new values, and this gives you the opportunity to test the program further.

  5. Click the Step Into button two times to display the Debug Test form again.

    Notice that the Text property of the TextBox2 object has been changed, as you directed, but the Text property of the TextBox1 object still holds a value of 20 (not 17). This is because you changed the Age variable in the program, not the property that assigned a value to Age. Your screen looks like this:

    graphic

The Immediate window has many uses—it provides an excellent companion to the Watch window, and it can help you experiment with specific test cases that might otherwise be very difficult to enter into your program.

Switching to the Command Window

The text-based Command window offers a complement to the Visual Studio Immediate window. Reminiscent of the MS-DOS command prompt, it can be used to run interface commands in the Visual Studio IDE. For example, entering the File.SaveAll command in the Command window saves all the files in the current project. (This command is the equivalent of the Save All command on the File menu.) If you already have the Immediate window open, you can switch between the Immediate and the Command windows by entering the >cmd and immed commands, respectively. You can also click the Command Window button on the right side of the Standard toolbar to open the Command window. You'll practice using the Command window in the following exercise.

Run the File.SaveAll command

  1. In the Immediate window, type >cmd, and then press Enter to switch to the Command window.

    The Command window appears, and the Immediate or Watch window might now be partially (or totally) hidden. (You can return to the Immediate window by clicking its tab or typing immed in the Command window.) The > prompt appears, a visual clue that you are now working in the Command window.

  2. Type File.SaveAll in the Command window, and then press Enter.

    As you begin typing File, all the Visual Studio commands associate with the File menu and file operations appear in a pop-up list box. This “command completion” feature offers a useful way to learn about the many commands that can be executed within the Command window. After you type File.SaveAll and press Enter, Visual Studio saves the current project, and the command prompt returns, as shown in the following illustration:

    graphic

  3. Experiment with other commands now if you like. (Begin your commands with menu names to discover the different commands available.) When you're finished, click the Close button in both the Command and Immediate windows. You're finished with them for now.



Microsoft Visual Basic 2005 Step by Step
Microsoft Visual Basic 2005 Step by Step (Step by Step (Microsoft))
ISBN: B003E7EV06
EAN: N/A
Year: 2003
Pages: 168

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net