Closer Look: What If... Testing Code in the Immediate Window

In this lesson, you will learn how to evaluate and run code using the Immediate window.

In the previous lesson, you learned how to fix run-time errors using the Exception Assistant. Sometimes, however, it may not be clear how to fix an error, and you may want to test a possible fix without changing the actual code. A special debugging window, the Immediate window, allows you to do that and more.

The Immediate Window

When your program is in break mode, the Immediate window can be used to run pieces of code and to evaluate variables or expressions. For example, if a run-time error occurs because of an empty variable, you can check the value of a variable. You can also use the Immediate window to assign that variable a value, and check how the rest of the program runs as a result.

Tip

When running your program in debug mode, you can put your program into break mode at any time by selecting Break from the Debug menu.

You can execute code in the Immediate Window by typing it in as you would in the Code Editor, and then pressing ENTER. To evaluate a variable or expression, type a question mark followed by the variable or expression you want to evaluate, and then press ENTER—the result is then displayed on the following line.

Try It!

To test code in the Immediate window

  1. On the File menu, choose New Project.

  2. On the Templates pane, in the New Project dialog box, click Windows Application.

  3. In the Name box, type Immediate, and then click OK.

    A new Windows Forms project opens.

  4. From the Toolbox, drag two TextBox controls and a Button control onto the form.

  5. Double-click the button to open the Code Editor.

  6. In the Button_Click event handler, add the following code.

    Dim miles As Integer = 0
    Dim hours As Integer = 0
    Dim speed As Integer = 0
    
    miles = CInt(Textbox1.Text)
    hours = CInt(Textbox2.Text)
    speed = miles / hours
    MsgBox(CStr(speed) & " miles per hour")
    
  7. Press F5 to run the program. Enter 100 in the first text box, and then enter 0 in the second text box.

  8. Click Button1. The program stops, and the Exception Assistant dialog box appears with the message "OverflowException was unhandled".

  9. In the Immediate window at the bottom of the IDE, type ?miles, and then press ENTER.

    The value 100 should appear on the next line.

    Tip

    You can open the Immediate window at any time by choosing Windows, Immediate from the Debug menu.

  10. Type ?hours, and then press ENTER.

    The value 0 should appear on the next line.

  11. Type hours = 4, and press ENTER. Then type ?hours, and press ENTER.

    Notice that the value of hours is now 4, the value you entered on the previous line. You could change the value of hours in the Immediate window without changing the program's code.

  12. Press F5 to continue. A message box is displayed with the result.

    Tip

    To prevent this run-time error from occurring, add an error handler to check for a valid number in the Try block, and then display a message to the user in the Catch block. For more information on error handlers, see What To Do When Something Goes Wrong: Handling Errors.

Next Steps

In this lesson, you learned how to use the Immediate window to check values and run code. In the next lesson, you will learn how to find and fix logic errors.

Next Lesson: What? It Wasn't Supposed To Do That! Finding Logic Errors

See Also

Tasks

It Doesn't Work! Finding and Eliminating Run-Time Errors

What To Do When Something Goes Wrong: Handling Errors