The VBScript Network and Systems Administrator's Cafe

May 26 2008   3:48PM GMT

VBScript Statements: Explanation of the Option Explicit Statement



Posted by: Jerry Lees
Tags:
option
option explicit
VBScript
VBScript Statements
vbscriptstatements

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.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

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: