Option Explicit and Option Strict in VB.NET are statements for the compiler. As a programmer always wants to discover the error in the program compile time, but not willing to discover in the running period. When a program is successfully compiled, you may think that the program has been checked by all compile periods, and there will be no more other errors to make you scared. However, in fact, the program runs with no errors and does not indicate whether the next runtime will have an error. Therefore, if the potential period of time period is timely discovery, the application will become very stable.
Option Explicit statement determines whether the compiler requires all variables to be explicitly declared, the syntax is as follows:
Option expedition [on | OFF]
If ON is selected, whether the compiler requires all variables that are explicitly declared. If OFF is selected, the compiler is an implicit declaration that allows variables. In this way, it has not been a good thing. For example, in the process of writing, accidentally knocking the wrong variable name, because the compiler allows the implicit declaration of the variable, it will not have errors when the program is compiled, but An unpredictable error may occur during the running period. Therefore, it is best not to use Option Explicit Off.
The OPTION STRICT statement determines the implicit conversion requirements of the compiler type, the syntax is as follows:
Option strict [ON | OFF]
If ON is selected, the compiler allows the relaxed implicit type conversion, and if Off, the compiler is a type conversion that is allowed to relax, but also allows narrow type conversion. For relaxed type conversions, the data loss or incorrect results will generally do not occur. For example, a variable that converts a constituting variable to a long (long) variable is a relaxed type conversion, and the long integer can contain a value of all integer variables. Conversely, conversion from long integer to integer variables is a narrow type conversion because some long integer values are excess the range represented by integer.
VB.NET default relaxation type conversion. For example, in the second row of the following code, the relaxed type conversion will be automatically connected, and the value of the value of the value is automatically converted to a long-intensive variable so that it can be saved to the variable B.
Dim a as integer = 5
DIM B As long = a
The type conversion of automatically occurs, called "implicit type conversion".
Now consider the opposite situation:
DIM A as long = 5
DIM B AS Integer = a
The second line of the above code, trying to make implicit narrowed type conversion. Whether the compiler allows such conversion to determine the setting of the Option Strict, if the option strict is set to ON, trying to make an implicit narrowing type conversion will compile errors; set to OFF, the compiler will automatically perform type conversion in the background . This will cause the program to throw an exception if the value of the actual conversion exceeds the range of the target type.
For some programs, the type conversion that sometimes narrows is necessary. Therefore, the programmer may know that when a variable is transformed, it will exceed the scope of the target type, and the appropriate code is added to handle the possible exception. This situation can be explicitly converted by appropriate functions, for example:
DIM A as long = 5
DIM B AS Integer = CINT (a)
The above transformation is called "explicit type conversion" because the programmer clearly requires integer transformation. Note that although from the surface, the above code is more than one function call compared to the previous "implicit type conversion", but the compiled code is the same, so the efficiency and implicit type conversion are the same.