Option Explicit Statement

advertisement
CS0004
VB.NET
Option Explicit Statement
Used at file level to force explicit declaration of all variables in that file.
Option Explicit { On | Off }
On
Optional. Enables Option Explicit checking. If On or Off is not specified after the Option
Explicit statement, the default is On.
Off
Optional. Disables Option Explicit checking.
Remarks
If used, the Option Explicit statement must appear in a file before any other source statements.
When Option Explicit appears in a file, you must explicitly declare all variables using the Dim,
Private, Public, or ReDim statements. If you attempt to use an undeclared variable name, an error
occurs at compile time. This avoids the problem of Variants.
If you don't use the Option Explicit statement, all undeclared variables are of Object type.
Note 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. If you do not specify Option Explicit
in your code, the compiler default is Option Explicit On.
Example
This example uses the Option Explicit statement to force explicit declaration of all variables.
Attempting to use an undeclared variable causes an error at compile time. The Option Explicit
statement is used at the module level only.
Option Explicit On
' Force explicit variable declaration.
Dim MyVar As Integer
' Declare variable.
MyInt = 10
' Undeclared variable generates error.
MyVar = 10
' Declared variable does not generate error.
D:\106737370.doc
CS0004
VB.NET
Option Strict Statement
Restricts implicit data type conversions to only widening conversions. This explicitly disallows any
data type conversions in which data loss would occur and any conversion between numeric types
and strings.
Option Strict { On | Off }
On
Optional. Enables Option Strict checking. If On or Off is not specified after the Option
Strict statement, the default is Off.
Off
Optional. Disables Option Strict checking.
Remarks
When used, the Option Strict statement must appear before any other code.
Visual Basic.NET generally allows implicit conversions of any data type to any other data type. Data
loss can occur when the value of one data type is converted to a data type with less precision or
smaller capacity, however, a run-time error message will occur if data will be lost in such a
conversion. Option Strict ensures compile-time notification of these types of conversions so they
may be avoided.
In addition to disallowing narrowing conversions, Option Strict generates an error for late binding.
An object is late bound when it is assigned to a variable that is declared to be of type Object.
Note The compiler default is Option Strict Off if do not specify Option Strict in your code.
Example
This example demonstrates how the Option Strict statement disallows late binding and conversions
where data would be lost.
Option Strict On
' Force explicit variable declaration.
Dim MyVar As Integer
Dim Obj As Object
MyVar = 1000
' Declare variables.
' Declared variable does not generate error.
'Attempting to convert to an Integer generates an error.
MyVar = 1234567890.987654321
'
'Call Obj.Method1()
' Late-bound call generates an error
D:\106737370.doc
Download