Option Explicit Statement

Forces explicit declaration of all variables in a script.

Option Explicit 

Remarks

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

When you use the Option Explicit statement, you must explicitly declare all variables using the Dim, Private, Public, or ReDim statements. If you attempt to use an undeclared variable name, an error occurs.

Note

   If you do not use Option Explicit, you can declare a variable implicitly just by using its name in your script. However, this is not generally a good practice because you could misspell the variable name in one or more locations, which causes unexpected results when the script is run. For that reason, the Option Explicit statement is available to require explicit declaration of all variables.

The following example illustrates use of the Option Explicit statement.

' The Option Explicit statement must be the first
' statement in your script.
' Force explicit variable declaration.
Option Explicit

Dim MyVar

' Because MyVar is declared, this statement does not generate an error.
MyVar = 10

' Because MyInt is undeclared, this statement generates an error.
MyInt = 10

Requirements

Version 1

See Also

Concepts

VBScript Variables

Change History

Date

History

Reason

April 2009

Added comments to example and modified note.

Information enhancement.