Thursday, January 5, 2012

Language Compile Options for the VB.NET Compiler

There are four language compile option for VB.NET:

This option declares the default comparison method used when comparing string data. There are two choices: binary and text

If the binary comparison method is chosen, the strings will be compared according to their binary representation. This method is the fastest, but may lead sometimes to some unwanted results if you use the Unicode set. This happens because the strings are compared character by character according to their Unicode value which would be something like:
A < B < E < Z < a < b < e < z < À < Ê < Ø < à < ê < ø
So, if you think that À should come after A and before B, you should not use the binary comparison method. Also binary comparison is case sensitive.

If the text comparison method is chosen, the strings will be compared according based on a case insensitive text sort order determined by the system's locale. This is something like:
(A=a) < (À = à) < (B=b) < (E=e) < (Ê = ê) < (Z=z) < (Ø = ø)
Console.Write("À" < "B")
'Output: True (when using Text Compare)
'Output: False (when using Binary Compare)
If this option is enabled all variables must be declared using the Dim or ReDim statements. If one tries to use an undeclared variable, an error will occur at compile time. If this option is disabled, one can use a variable without declaring it first using Dim or ReDim.
i = 3
'Will trigger an error if Option Explicit is On
'Will not trigger an error if Option Explicit is Off
When Option Strict is On, implict narrowing conversions, late binding and implicit typing that results to Object will cause a compile-time error. If Option Strict is Off, you will receive warnings or run-time errors instead of compile-time errors;
'Narrowing conversion example
Dim a As Long = 5
Dim b As Integer = a
'Option Strict On => Will cause error, can be corrected by
'Dim b As Integer = Ctype(a,Integer)
'Option Strict Off => Will raise a warning

'Late binding example
Dim text As Object
text = New Object
text = "abc"
'Option Strict On => Will cause compile error
'Option Strict Off => Will cause a runtime error

'Implicit typing that results to Object example
Dim x
'Option Strict On => Will cause compile error
'Option Strict Off => x will be equal to Nothing
When Option Infer is On, the compiler will guess (infer) the type of an object if the type is unspecified according to a value assigned to it. When Option Infer is Off, the compiler will not try to guess the type of the object if you don't specify it and will cast it as Object. (if Option Strict is On this may cause compile-time errors because this is an implicit typing that results to Object).
Dim number = 3
Dim text = "abc"
Dim letter = "c"c
Dim real = Math.PI
'If Option Infer is On
'number will be of type int
'text will be of type string
'letter will be of type char
'real will be of type double
'If Option Infer is Off
'number,text,letter,real will be considered as Object
To set the compile options directly from Visual Studio, one must go to the project's properties (either by right-clicking on your project and selecting the Properties item or by going toProject -> <name_of_your_project> Properties in the menu bar) and then to the compile panel.
In command line, you simply need to specify the options as arguments to the compiler:
Alternatively you can specify this options directly into the code. Using them this way, the new options will overwrite the old options specified as compiler arguments (or set in Visual Studio).
Option Strict Off
Option Infer On
Option Explicit Off
Option Compare Binary
Module Program
    Public Sub Main(ByVal args As String())
        Console.Write("Hello compiler world!")
    End Sub
End Module

No comments:

Post a Comment

Got a question regarding something in the article? Leave me a comment and I will get back at you as soon as I can!

Related Posts Plugin for WordPress, Blogger...
Recommended Post Slide Out For Blogger