Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

Finding and Getting Rid of Compiler Errors

In this lesson, you will learn how to find and fix compiler errors.

As you learned in the previous lesson, compiler errors occur when the Visual Basic compiler comes across unrecognizable code—usually because you made a mistake when typing. Because compiler errors prevent a program from running, you need to find and fix, or debug, those errors before trying to run your program.

Finding compiler errors is actually pretty easy, since the program cannot run until errors are fixed. When you press F5, if there are any compiler errors, you see a dialog box that reads "There were build errors. Continue?". If you choose Yes, the last error-free version of the program will run; if you choose No, the program stops and the Error List window appears.

The Error List window displays information about the compiler error, including a description of the error and its location in your code. If you double-click the error in the Error List, the offending line of code is highlighted in the Code Editor. You can also press F1 to display Help and get more information about the error and how to fix it.

The Visual Basic Code Editor can also help you to find and fix compiler errors before you even try to run the program. Using a feature called IntelliSense, Visual Basic inspects your code as you type—if it finds code that will cause a compiler error, it underlines that code with a wavy blue line. If you hold the mouse over that line, a message describing the error is displayed. If the Error List window is visible, the error message is displayed there, as well.

To find and fix compiler errors

  1. On the File menu, select New Project.

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

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

    A new Windows Forms project opens.

  4. Double-click the form to open the Code Editor.

  5. In the Form_Load event handler, add the following code.

    
    End If
    
    
    
  6. Press ENTER. You will see a wavy blue line underneath End If.

    If you hold the mouse over the line, you will see the message "'End If' must be preceded by a matching 'If'."

  7. Change the code to look like the following.

    
    If 1 < 2 Then
    End If
    
    
    

    Note that the wavy blue line has disappeared.

  8. Add the following new line of code after the If...Then statement.

    
    MgBox("Hello")
    
    
    
  9. Press F5 to run the program. A dialog box appears with the message "There were build errors. Would you like to continue and run the last successful build?"

  10. Click No. The Error List window displays the error message "Name 'MgBox' is not declared."

  11. Double-click the error message in the Error List, and change the code to MsgBox("Hello").

  12. Press F5 again. This time the program should run, causing a message box to appear.

In this lesson, you learned how to find and fix compiler errors. In the next lesson, you will learn how to fix a different type of error—run-time errors. Next Lesson: It Doesn't Work! Finding and Eliminating Run-Time Errors

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.