Friday, October 26, 2007

Option Strict/Explicit/Compare

Option Explicit Statement (Visual Basic)
Forces explicit declaration of all variables in a file.

Option Explicit { On Off }
var ExpCollDivStr = ExpCollDivStr;
ExpCollDivStr = ExpCollDivStr + "ctl00_LibFrame_ctl039cf6c6b,";
var ExpCollImgStr = ExpCollImgStr;
ExpCollImgStr = ExpCollImgStr + "ctl00_LibFrame_ctl03img,";
Parts
On
Optional. Enables Option Explicit checking.

If On or Off is not specified, the default is On.

Off
Optional. Disables Option Explicit checking.

var ExpCollDivStr = ExpCollDivStr;
ExpCollDivStr = ExpCollDivStr + "ctl00_LibFrame_ctl043a3f538,";
var ExpCollImgStr = ExpCollImgStr;
ExpCollImgStr = ExpCollImgStr + "ctl00_LibFrame_ctl04img,";

Remarks
If used, the Option Explicit statement must appear in a file before any other source code statements.

When Option Explicit appears in a file, you must explicitly declare all variables using the Dim or ReDim statements. If you attempt to use an undeclared variable name, an error occurs at compile time.

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 use the Option Explicit statement, all undeclared variables are of Object type.

Note :
The compiler default is Option Explicit On if you do not specify Option Explicit in your code.
You can also set Option Explicit in the Visual Studio integrated development environment (IDE) or on a command line.

To set Option Explicit in the integrated development environment (IDE)
1. On the Tools menu, choose Options.
2. Open the Projects and Solutions node.
3. Choose VB Defaults.
4. Modify the Option Explicit setting.

To set Option Explicit on the command line
· Include the /optionexplicit compiler option in the vbc command.
var ExpCollDivStr = ExpCollDivStr;
ExpCollDivStr = ExpCollDivStr + "ctl00_LibFrame_ctl06ecb5365,";
var ExpCollImgStr = ExpCollImgStr;
ExpCollImgStr = ExpCollImgStr + "ctl00_LibFrame_ctl06img,";

Example
The following 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.
Visual Basic
Copy Code
' Force explicit variable declaration. Option Explicit On
Visual Basic
Copy Code
Dim thisVar As Integer
thisVar = 10
' The following assignment produces a COMPILER ERROR because ' the variable is not declared and Option Explicit is On.
thisInt = 10 ' causes ERROR




Visual Basic Language Reference
Option Strict Statement

Restricts implicit data type conversions to only widening conversions.

Option Strict { On Off }
var ExpCollDivStr = ExpCollDivStr;
ExpCollDivStr = ExpCollDivStr + "ctl00_LibFrame_ctl031d087ea,";
var ExpCollImgStr = ExpCollImgStr;
ExpCollImgStr = ExpCollImgStr + "ctl00_LibFrame_ctl03img,";
Parts

On
Optional. Enables Option Strict checking.

Off
Optional. Disables Option Strict checking.

If On or Off is not specified, the default is Off.

var ExpCollDivStr = ExpCollDivStr;
ExpCollDivStr = ExpCollDivStr + "ctl00_LibFrame_ctl04b969ba9,";
var ExpCollImgStr = ExpCollImgStr;
ExpCollImgStr = ExpCollImgStr + "ctl00_LibFrame_ctl04img,";

Remarks
If used, the Option Strict statement must appear in a file before any other source code statements.

Visual Basic allows conversions of many data types to other data types. Data loss can occur when the value of one data type is converted to a data type with less precision or smaller capacity.
A run-time error occurs if such a narrowing conversion fails. Option Strict ensures compile-time notification of these narrowing conversions so they can be avoided.

In addition to disallowing implicit 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.

Because Option Strict On provides strong typing, prevents unintended type conversions with data loss, disallows late binding, and improves performance, its use is strongly recommended.

Note :
The compiler default is Option Strict Off if you do not specify Option Strict in your code.
You can also set Option Strict in the Visual Studio integrated development environment (IDE) or on a command line.

To set Option Strict in the integrated development environment (IDE)
1. On the Tools menu, choose Options.
2. Open the Projects and Solutions node.
3. Choose VB Defaults.
4. Modify the Option Strict setting.

To set Option Strict on the command line
· Include the /optionstrict compiler option in the vbc command.
var ExpCollDivStr = ExpCollDivStr;
ExpCollDivStr = ExpCollDivStr + "ctl00_LibFrame_ctl060ac3ee2,";
var ExpCollImgStr = ExpCollImgStr;
ExpCollImgStr = ExpCollImgStr + "ctl00_LibFrame_ctl06img,";

Example
The following example demonstrates how the Option Strict statement disallows late binding and conversions where data would be lost.

Visual Basic
Copy Code
Option Strict On
Visual Basic
Copy Code
Dim thisVar As Integer
Dim thisObj As Object = New widget
thisVar = 1000
' Declared variable does not generate error.
' Attempting to convert Double to Integer generates a COMPILER ERROR.
thisVar = 1234567890.9876542 ' causes ERROR
' Late-bound call generates a COMPILER ERROR.
Call thisObj.Method1() ' causes ERROR





Visual Basic Language Reference
Option Compare Statement


Declares the default comparison method to use when comparing string data.

Option Compare { Binary Text }
var ExpCollDivStr = ExpCollDivStr;
ExpCollDivStr = ExpCollDivStr + "ctl00_LibFrame_ctl039e03175,";
var ExpCollImgStr = ExpCollImgStr;
ExpCollImgStr = ExpCollImgStr + "ctl00_LibFrame_ctl03img,";

Parts
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.

var ExpCollDivStr = ExpCollDivStr;
ExpCollDivStr = ExpCollDivStr + "ctl00_LibFrame_ctl047019bb8,";
var ExpCollImgStr = ExpCollImgStr;
ExpCollImgStr = ExpCollImgStr + "ctl00_LibFrame_ctl04img,";

Remarks
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) 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. For more information, see

Code Pages.
In the following example, characters in the English/European code page (ANSI 1252) 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 in the same code page are sorted using Option Compare Text, the following text sort order is produced.
(A=a) < (À = à) < (B=b) < (E=e) < (Ê = ê) < (Z=z) < (Ø = ø)
You can also set Option Compare in the Visual Studio integrated development environment (IDE) or on a command line.

To set Option Compare in the integrated development environment (IDE)
1. On the Tools menu, choose Options.
2. Open the Projects and Solutions node.
3. Choose VB Defaults.
4. Modify the Option Compare setting.

To set Option Compare on the command line
· Include the /optioncompare compiler option in the vbc command.
var ExpCollDivStr = ExpCollDivStr;
ExpCollDivStr = ExpCollDivStr + "ctl00_LibFrame_ctl07878f921,";
var ExpCollImgStr = ExpCollImgStr;
ExpCollImgStr = ExpCollImgStr + "ctl00_LibFrame_ctl07img,";

Example
The following example uses the Option Compare statement to set the binary comparison as the default string comparison method.

Visual Basic
Copy Code
' Set the string comparison method to Binary ("AAA" < "aaa").

Option Compare Binary

The following example uses the Option Compare statement to set the case-insensitive text sort order as the default string comparison method.
Visual Basic
Copy Code
' Set the string comparison method to Text ("AAA" = "aaa").

Option Compare Text

No comments: