If used, the Option Explicit statement must appear in a file before any other source code statements.
When Option Explicit appears in a file, you must explicitly declare all variables by using the Dim or ReDim statements. If you try to use an undeclared variable name, an error occurs at compile time.
Use Option Explicit to avoid incorrectly typing the name of an existing variable or to avoid confusion in code where the scope of the variable is not clear. If you do not use the Option Explicit statement, all undeclared variables are of Object type.
Note: |
|---|
The compiler default is
Option Explicit On if you do not specify Option Explicit in your code.
|
You can also set Option Explicit in the Visual Studio integrated development environment (IDE) or on a command line.
Note: |
|---|
The dialog boxes and menu commands you see might differ from those described in Help, depending on your active settings or edition. To change your settings, click
Import and Export Settings on the Tools menu. For more information, see Visual Studio Settings.
|
To set Option Explicit in the IDE
On the Tools menu, click Options.
Open the Projects and Solutions node.
Choose VB Defaults.
Modify the Option Explicit setting.
To set Option Explicit on the command line