Programming 笔记

工作中遇到的问题就记载这里

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
Option Strict on


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   ' Declare variables.
Dim Obj As Object

MyVar = 1000   ' 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




Option Explicit On








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.

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.
posted on 2005-11-30 05:06  IT 笔记  阅读(2294)  评论(2编辑  收藏  举报