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
On the File menu, select New Project.
On the Templates pane in the New Project dialog box, click Windows Application.
In the Name box, type CompilerErrors and then click OK.
A new Windows Forms project opens.
Double-click the form to open the Code Editor.
In the Form_Load event handler, add the following code.
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'."
Change the code to look like the following.
Note that the wavy blue line has disappeared.
Add the following new line of code after the If...Then statement.
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?"
Click No. The Error List window displays the error message "Name 'MgBox' is not declared."
Double-click the error message in the Error List, and change the code to MsgBox("Hello").
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