handout

advertisement
CSC110 - USING OPTION STATEMENTS IN AN APPLICATION
OPTION EXPLICIT
Used at file level to force explicit declaration of all variables in that file.
Option Explicit { On | Off }
In the past with VB6 this option was inherently off, but with VB.Net the default is that it is on. 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.
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.
OPTION STRICT
This is also used at the file level and the option will enable or disable strict type checking.
Option Strict { On | Off }
The Option Strict statement must appear at the beginning of a module although a comment may precede the Option Strict statement.
When enabled, strict type checking is performed for the module containing the Option Strict statement. The following type conversion
rules apply when strict type checking is enabled.

Strings will not be implicitly converted to numbers

Numeric data types will be implicitly converted to less restrictive types. For example, a Short will be implicitly converted to
an Integer or a Long, and a Single will be implicitly converted to a Double.

Implicit conversion of more restrictive type will cause a syntax error. For example, a Double will not be implicitly converted
to a Single, and a Long will not be implicitly converted to a Integer or Short.
OPTION COMPARE
Used at file level to declare the default comparison method to use when string data is compared.
Option Compare { Binary | Text }

Binary .................. Optional. Results in string comparisons based on a sort order derived from the internal binary
representations of the characters.

Text ...................... Optional. Results in string comparisons based on a case-insensitive text sort order determined by your
system's locale.
If used, the Option Compare statement must appear in a file before any other source statements.
The Option Compare statement specifies the string comparison method (Binary or Text) for a class, module or structure. If an Option
Compare statement is not included, the default text comparison method is Binary.
In Microsoft Windows, sort order is determined by the code page. In the following example, characters are sorted using Option
Compare Binary, which produces a typical binary sort order:
A<B<E<Z<a<b<e<z<À<Ê<Ø<à<ê<ø
When the same characters are sorted using Option Compare Text, the following text sort order is produced:
(A=a) < ( À=à) < (B=b) < (E=e) < (Ê=ê) < (Z=z) < (Ø=ø)
This example uses the Option Compare statement to set the default string comparison method. The Option Compare statement is used
at the module level only.
' Set the string comparison method to Binary.
Option Compare Binary ' That is, "AAA" is less than "aaa".
' Set the string comparison method to Text.
Option Compare Text ' That is, "AAA" is equal to "aaa".
Download