I always use option explicit, but what exatly does it do? I forgot where but it said that I should but Option Explicit in the declerations section, but I'm just wondering what does it do?
Printable View
I always use option explicit, but what exatly does it do? I forgot where but it said that I should but Option Explicit in the declerations section, but I'm just wondering what does it do?
It makes you so you have to Dim your varibles and if you don't it gives you an Ambiguous name error.
Look in your VB Help file for Option Explicit statement.
Here is what it says (if you don't have a help file):
Option Explicit Statement
Used at module level to force explicit declaration of all variables in that module.
Syntax
Option Explicit
Remarks
If used, the Option Explicit statement must appear in a module before any statements that declare variables or define constants.
If you don't use the Option Explicit statement, all undeclared variables are of Variant type unless the default type is otherwise specified with a Deftype statement.
When you use the Option Explicit statement, you must explicitly declare all variables using the Dim, Private, Public, ReDim, or Static statements. If you attempt to use an undeclared variable name, an error occurs at compile time.
Tip Use Option Explicit to avoid incorrectly typing the name of an existing variable or to avoid risking confusion in code where the scope of the variable is not clear.
It makes vb require that you Dim a variable before you could use it:
would cause a "Variable not defined" errorCode:Option Explicit
Sub Test()
For i = 1 To 10
DoSomething
Next i
End Sub
to fix it:Code:Option Explicit
Sub Test()
Dim i%
For i = 1 To 10
DoSomething
Next i
End Sub
Why do people not read the previous posts?
Sometimes because it hasn't appeared yet (being written at the same time)
but mostly because of a short attention span and a desire to get to the bottom of the thread :D