Permalink
Fetching contributors…
Cannot retrieve contributors at this time
100 lines (77 sloc) 4.49 KB
title ms.date ms.prod ms.reviewer ms.suite ms.technology ms.topic f1_keywords dev_langs helpviewer_keywords ms.assetid caps.latest.revision author ms.author translation.priority.ht
Option Explicit Statement (Visual Basic)
2015-07-20
.net
devlang-visual-basic
article
vb.Explicit
vb.OptionExplicit
VB
declaring variables, explicit
forced variable declaration in Option Explicit statement
Explicit keyword
explicit variable declaration
Option Explicit statement
e82ac1ad-2cd3-49b2-b985-8bcf016f3fcc
17
dotnet-bot
dotnetcontent
cs-cz
de-de
es-es
fr-fr
it-it
ja-jp
ko-kr
pl-pl
pt-br
ru-ru
tr-tr
zh-cn
zh-tw

Option Explicit Statement (Visual Basic)

Forces explicit declaration of all variables in a file, or allows implicit declarations of variables.

Syntax

Option Explicit { On | Off }  

Parts

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

Off
Optional. Disables Option Explicit checking.

Remarks

When Option Explicit On or 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. The Option Explicit Off statement allows implicit declaration of variables.

If used, the Option Explicit statement must appear in a file before any other source code statements.

[!NOTE] Setting Option Explicit to Off is generally not a good practice. You could misspell a variable name in one or more locations, which would cause unexpected results when the program is run.

When an Option Explicit Statement Is Not Present

If the source code does not contain an Option Explicit statement, the Option Explicit setting on the Compile Page, Project Designer (Visual Basic) is used. If the command-line compiler is used, the /optionexplicit compiler option is used.

To set Option Explicit in the IDE

  1. In Solution Explorer, select a project. On the Project menu, click Properties. For more information, see Introduction to the Project Designer.

  2. Click the Compile tab.

  3. Set the value in the Option Explicit box.

When you create a new project, the Option Explicit setting on the Compile tab is set to the Option Explicit setting in the VB Defaults dialog box. To access the VB Defaults dialog box, on the Tools menu, click Options. In the Options dialog box, expand Projects and Solutions, and then click VB Defaults. The initial default setting in VB Defaults is On.

To set Option Explicit on the command line

Example

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.

[!code-vbVbVbalrStatements#47]

[!code-vbVbVbalrStatements#48]

See Also

Dim Statement
ReDim Statement
Option Compare Statement
Option Strict Statement
/optioncompare
/optionexplicit
/optionstrict
Visual Basic Defaults, Projects, Options Dialog Box