Sunday, March 29, 2009

The use of Option Exlicit in VB program

The use of Option Explicit is to help us to track errors in the usage of variable names within a VB program code. For example, if we commit a typo, Visual Basic will pop up an error message “variable not defined”. Indeed, Option Explicit forces the programmer to declare all the variables using the Dim keyword. It is a good practice to use Option Explicit because it will prevent us from using incorrect variable names due to typing errors, especially when the program gets longer. With the usage of Option Explicit, it will save our time in debugging our programs. For example, In the following program, a typo was committed, EmployeID, which was declared as EmployeeID.

Option Explicit
Private Sub Command1_Click()
Dim EmployeeID As String
EmployeID = "John"
Text1.Text = EmployeeID
End Sub

When you run the program, a dialog box with an error message 'Variable not defined" will appear ,when you click the OK button, it will show the highlighted typo, enabling us to correct the mistake.

No comments: