开发者

I'm not sure what Option explicit means? [duplicate]

开发者 https://www.devze.com 2022-12-30 23:43 出处:网络
This question already has answers here: Closed 12 years ago. Possible Duplicate: what’s an option strict and explicit?
This question already has answers here: Closed 12 years ago.

Possible Duplicate:

what’s an option strict and explicit?

Is it about case sensitivity? Complete noo开发者_StackOverflow中文版b here.


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.


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
0

精彩评论

暂无评论...
验证码 换一张
取 消