Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2005
Visual Studio
Visual Basic
Statements
F-P Statements
 Option Explicit Statement

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:

Want more? Here are some additional resources on this topic:

Visual Basic Language Reference 
Option Explicit Statement (Visual Basic) 

Forces explicit declaration of all variables in a file.

Option Explicit { On | Off }
On

Optional. Enables Option Explicit checking. If On or Off is not specified, the default is On.

Off

Optional. Disables Option Explicit checking.

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 using the Dim or ReDim statements. If you attempt 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.

NoteNote

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.

To set Option Explicit in the integrated development environment (IDE)

  1. On the Tools menu, choose Options.

  2. Open the Projects and Solutions node.

  3. Choose VB Defaults.

  4. Modify the Option Explicit setting.

To set Option Explicit on the command line

The following example uses the Option Explicit statement to force explicit declaration of all variables. Attempting to use an undeclared variable causes an error at compile time.

Visual Basic
' Force explicit variable declaration.
Option Explicit On
Visual Basic
Dim thisVar As Integer
thisVar = 10
' The following assignment produces a COMPILER ERROR because
' the variable is not declared and Option Explicit is On.
thisInt = 10 ' causes ERROR
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker