1

Possible Duplicate:
what’s an option strict and explicit?

Is it about case sensitivity? Complete noob here.

Community
  • 1
  • 1
phearn
  • 11
  • 2
  • 2
    Option Explicit is the single one line of code I taught my students that if they forgot that on their final exam, they would fail, regardless of the quality of the rest of the code. But yeah, duplicate. – Lasse V. Karlsen May 18 '10 at 19:47
  • @LasseV.Karlsen This question was about VBScript, the duplicate is to do with VB.Net which leads to confusion as the syntax for declaring variables is different. – user692942 Jun 17 '21 at 10:16
  • That might be true, but it's still the same core answer, it forces you to declare all variables. – Lasse V. Karlsen Jun 17 '21 at 10:33
  • @LasseV.Karlsen still confusing for those looking for VBScript specific answers, you only have to look at [answer given here](https://stackoverflow.com/a/2860637/692942) adding to confusion with VB.Net examples. Also VBScript has no concept of `Option Strict` just `Option Explicit`. – user692942 Jun 20 '21 at 08:15

2 Answers2

3

According to MSDN:

Used at file level to force explicit declaration of all variables in that file.

Otherwise, you can just use a variable without having to declare it first.

They even included an example:

Option Explicit On   ' Force explicit variable declaration.
Dim MyVar   ' Declare variable.
MyInt = 10   ' Undeclared variable generates error.
MyVar = 10   ' Declared variable does not generate error.
user692942
  • 16,398
  • 7
  • 76
  • 175
Justin Ethier
  • 131,333
  • 52
  • 229
  • 284
  • it lets you "declare" variables just by using it for the first time. In the sample above, MyInt becomes a variable just by using it without a Dim statement. ALWAYS use Option Explicit if you can, it will save you many headaches. Dim is an explicit variable declaration, MyInt above is an implicit one. – Jeremy May 18 '10 at 19:49
0

When option explicit is off visual basic allows you to implicitly declare a variable by assigning a value to it. This is a really bad idea as misspelling a variable name would silently create a new variable causing a very hard to find bug.

Option Explicit Off
Imports System
Public Class ImplicitVariable
 Public Shared Sub Main()
  a = 33
  Console.WriteLine("a has value '{0}' and type {1}", a, a.GetType())
 End Sub
End Class
Andy
  • 9
  • 1