Udostępnij za pośrednictwem


Implicit and Explicit Declaration

By default, the Visual Basic compiler enforces explicit declaration, which requires that you declare every variable before you use it. You can remove this requirement and permit implicit declaration.

Visual Basic provides a switch that controls explicit declaration. By default, this switch is set to On, and the compiler enforces explicit declaration. If you turn this switch Off, you can use variables without declaring them.

Explicit Declaration Switch. You can set the explicit declaration switch On or Off in any of the following ways:

  • Set the appropriate project property in the integrated development environment (IDE). Click <ProjectName> Properties from the Project menu, and then click the Compile tab. You can set the default values for Option explicit, Option strict, and Option compare.

  • Specify the /optionexplicit command-line compiler option.

  • Include the Option Explicit Statement (Visual Basic) at the beginning of your code.

If you use the OptionExplicit statement, that setting overrides both the project property and compiler option settings, but only for the source code file in which it appears.

Performance Advantage. Setting OptionExplicit to On has the advantage of forcing type inference to be made at compile time instead of run time. This improves performance.

Implicit Declaration

If you set OptionExplicit to Off, you can implicitly declare a variable by simply using it in your code. The compiler assigns the Object Data Type to all implicitly declared variables. However, your application is more efficient if you declare all your variables explicitly and with a specific data type. This reduces the incidence of naming-conflict errors and spelling mistakes. It also lets the compiler detect potential run-time errors such as assigning an Integer to a Short.

Potential Errors

Unintended New Variables

You can write a procedure in which you do not declare a local variable. The following example illustrates this.

Function safeSqrt(num)
' Make sure num is positive for square root.
    tempVal = Math.Abs(num)
    Return Math.Sqrt(tempVal)
End Function

Visual Basic automatically creates tempVal as a local variable, which you can use as if you had declared it explicitly. While this is convenient, it can lead to subtle errors in your code if you misspell a variable name. Suppose you had written the procedure in the preceding example as follows:

Function safeSqrt(num)
' Make sure num is positive for square root.
    tempVal = Math.Abs(num)
    Return Math.Sqrt(temVal)
End Function

At first glance, this code looks the same. But because the tempVal variable is misspelled as the argument to Sqrt, the compiler creates an additional local variable called temVal, which is never assigned a value, and your function always returns zero.

Unintended Existing Element Reference

When Visual Basic encounters a new name, it cannot determine whether you meant to declare a new variable implicitly, or whether you misspelled an existing variable name. Therefore it attempts to create a new variable with that name. There might be a variable or other programming element already defined with that name, and your code would use that definition unintentionally.

You can avoid problems with misnamed variables by using explicit declaration.

Explicit Declaration

Had explicit declaration been in effect for the source file containing the safeSqrt procedure in the preceding example, Visual Basic would have recognized tempVal and temVal as undeclared variables and generated errors for both of them. As a result, you would then explicitly declare tempVal. The following example illustrates this.

Function safeSqrt(ByVal num As Double) As Double
' Make sure num is positive for square root.
    Dim tempVal As Double = Math.Abs(num)
    Return Math.Sqrt(temVal)
End Function

With this revised code, you would understand the problem immediately because Visual Basic would display an error message for the incorrectly spelled temVal. Because explicit declaration helps you catch these kinds of errors, it is recommended that you use it with all your code.

Note

The OptionExplicit statement operates on a file-by-file basis. It must be at the beginning of every source code file in which you want to control the enforcement of explicit variable declaration.

See Also

Tasks

How to: Modify Project Properties and Configuration Settings

Concepts

Typeless Programming in Visual Basic

Type Checking in Visual Basic

Object as the Universal Data Type

Efficient Use of Data Types

Variables in Visual Basic

Visual Basic Naming Conventions

Reference

Data Type Summary (Visual Basic)

Type Conversion Functions