Option Infer Statement
Enables the use of local type inference in declaring variables.
Option Infer { On | Off }
To set Option Infer in a file, type Option Infer On or Option Infer Off at the top of the file, before any other source code. If the value set for Option Infer in a file conflicts with the value set in the IDE or on the command line, the value in the file has precedence.
When you set Option Infer to On, you can declare local variables without explicitly stating a data type. The compiler infers the data type of a variable from the type of its initialization expression.
In the following illustration, Option Infer is turned on. The variable in the declaration Dim someVar = 2 is declared as an integer by type inference.

In the following illustration, Option Infer is turned off. The variable in the declaration Dim someVar = 2 is declared as an Object by type inference. In this example, the Option Strict setting is set to Off on the Compile Page, Project Designer (Visual Basic).

When a variable is declared as an Object, the run-time type can change while the program is running. Visual Basic performs operations called boxing and unboxing to convert between an Object and a value type. These operations make execution slower. For information about boxing and unboxing, see the Visual Basic Language Specification.
Type inference applies at the procedure level, and does not apply outside a procedure in a class, structure, module, or interface.
Option Compare Statement
Declares the default comparison method to use when comparing string data.
Term | Definition |
Binary | Optional. Results in string comparisons based on a sort order derived from the internal binary representations of the characters. This type of comparison is useful especially if the strings can contain characters that are not to be interpreted as text. In this case, you do not want to bias comparisons with alphabetical equivalences, such as case insensitivity. |
Text | Optional. Results in string comparisons based on a case-insensitive text sort order determined by your system's locale. This type of comparison is useful if your strings contain all text characters, and you want to compare them taking into account alphabetic equivalences such as case insensitivity and closely related letters. For example, you might want to consider A and a to be equal, and Ä and ä to come before B and b. |
If used, the Option Compare statement must appear in a file before any other source code statements.
The Option Compare statement specifies the string comparison method (Binary or Text). The default text comparison method is Binary.
A Binary comparison compares the numeric Unicode value of each character in each string. A Text comparison compares each Unicode character based on its lexical meaning in the current culture.
In Microsoft Windows, sort order is determined by the code page. For more information, see Code Pages.
In the following example, characters in the English/European code page (ANSI 1252) are sorted by using Option Compare Binary, which produces a typical binary sort order.
A < B < E < Z < a < b < e < z < À < Ê < Ø < à < ê < ø
When the same characters in the same code page are sorted by using Option Compare Text, the following text sort order is produced.
(A=a) < (À = à) < (B=b) < (E=e) < (Ê = ê) < (Z=z) < (Ø = ø)
Restricts implicit data type conversions to only widening conversions, disallows late binding, and disallows implicit typing that results in an Object type.
Option Strict Statement
When Option Strict On or Option Strict appears in a file, the following conditions cause a compile-time error:
-
Implicit narrowing conversions
-
Late binding
-
Implicit typing that results in an Object type
Option Explicit Statement (Visual Basic)
Forces explicit declaration of all variables in a file, or allows implicit declarations of variables.
No comments:
Post a Comment