The VBScript Network and Systems Administrator's Cafe

May 26 2008   3:48PM GMT

VBScript Statements: Explanation of the Option Explicit Statement

Jerry Lees Jerry Lees Profile: Jerry Lees

In VBScript declaring your variables up front is not required (as stated in this sites explanation of the Dim Statement), however, many programmers choose to declare their variables up front to avoid having to track down misspellings in their code. They go further by adding the Option Explicit statement to force them to do so. With Option Explicit turned on and error is thrown when a variable is used before being declared.

Consider the following code, that doesn’t use Option Explicit:

TheFirstVariable = 1
TheSecondVariable = 2
TheThirdVariable = TehFirstVariable + TheSecondVariable

Wscript.echo TheThirdVariable

The output will be 2, when you were expecting 3! To find the error, if you didn’t notice it, read the third line very slowly TheFirstVariable is spelled differently as TehFirstVariable— therefore creating a new variable with a default value of zero. (or empty if used as a string)

 Comment on this Post

There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

Share this item with your network: