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 }
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 the conditions described above, Option Strict generates an error for:
This example uses the Option Strict statement to show how a declared variable can be converted to a data type of a larger capacity. Attempting to use an undeclared variable causes an error at compile time.
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. MyVar = 1234567890.987654321 ' 'Attempting to convert to an Integer will generate an error. MyInt = 10 ' Undeclared variable generates error in Option Strict mode. Call Obj.Method1() ' Late-bound call generates an error