Pages

Friday, 24 August 2012

What is use of Option Explicit statement?


·         The Option Explicit statement forces the explicit declaration of all variables using the Dim, Private, Public, ReDim, etc statements.
·         In a long program, this statement prevents the accidental reuse of the name of a previously declared variable. Also, if you mistype/typos a declared variable's name or try to use an undeclared variable, an error message is generated. 
·         By default the Option Explicit is On.
·         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.

1) Option Explicit On Example:
Option Explicit On
Dim intVar As Integer
intVar = 10
intUndeclare = 10  'causes complie time ERROR due to 'intUndeclare' not declare and Option Explicit is On.

2) Option Explicit Off Example:
Option Explicit Off
Dim intVar As Integer
intVar = 10
intUndeclare = 10  Here ‘intUndeclare’ is not declared , because the Option Explicit Of, without any compiler error you can continue the program. 


No comments:

Post a Comment