Visual Basic 2008 9.0 .NET Examples and Ebook

New in Visual Basic 2008 - 9.0

Introduction to Visual Basic

Volgend Onderwerp

Local Type Inference

Vorig Onderwerp

Partial Methods

|

Object Initializers

Volgend Onderwerp
Early Bound and Strongly Typed Objecten

Early Bound and Strongly Typed Objecten

Option Infer

Option Infer



The compiler can derive the type of a local variable, declared without type specifier ( As clause ), from the static type of the initialization expression.


Option Infer On
Option Explicit Off
Option Strict Off
Class Example1
    Public Shared Sub Main()
        Dim value1 = 1
        Dim value2 = "2"
        Dim value3 = "3"c
        Dim value4 = GetSomeByte()
        Dim value5 = New Integer() {1, 2, 3}
        For Each value6 In value5
            ' ...
        Next
        Static value7 = 4
        '
        Console.ReadLine()
    End Sub
    Public Shared Function GetSomeByte() As Byte
    End Function
End Class
Download Broncode

The static types of the variables are :


 value1 -> 'Int32'
 value2 -> 'String'
 value3 -> 'Char'
 value4 -> 'Byte'
 value5 -> 'Integer()'
 value6 -> 'Integer'
 value7 -> 'Object'

Static variables have a longer lifespan than normal local variables. Local type inference is not applied on Static variables.


Early Bound and Strongly Typed Objecten


It was possible to declare variables without type specifier in Visual Basic 8.0 ( 2005 ) when permissive type semantics ( Option Strict Off ) were used. The "default datatype" Object was used in these situations.
Several negative effects can occure due to this Object datatype, for instance (un)boxing and calls on late bound objects.
A call made on a late bound object can only be bound ( message binding ) to an implementation at runtime ( after compiletime ). This creates a performance overhead, and introduces the risk on type-errors at runtime.

Variables declared without type specifier in Visual Basic 9.0 ( 2008 ) will, due to local type inference, be strongly typed. This makes more objects early bound.
The compiler now can catch errors which would occure when members are called on objects without support for those members.
The compiler can also perform better optimalizations ( for instance memory allocations ) for these strongly typed objects.


Visual Studio : Strongly typed expressions also offer us the benefits of ItelliSense in the Visual Studio IDE.


Klik hier om terug naar boven te gaan.  Up



Option Infer


Visual Studio : 'Option Infer On' is the default for project created with Visual Studio 2008.

When a Visual Studio 2003 or 2005 project is imported into Visual Studio 2008, 'Option Infer' is set to 'Off'. Logical choise because variables declared without type specifier were of type 'Object', and must stay that way to avoid compile-errors.


Local type inference can be switch off with Option Infer Off.

Local type inference is only applied to local variables, not to arguments or fields of a class.

Following example is executed under Option Strict Off, Option Explicit Off and Option Infer On.
Try to predict what the static ( at compiletime ) and dynamic ( at runtime ) types of the variables will be.


Class Example2
    Private Shared variable1
    Private Shared variable2 = "a"
    Public Shared Sub Main()
        variable1 = 1
        Dim variable3
        variable3 = 2S
        Dim variable4 = "b"c
        variable5 = "c"
        Dim variable6 = SomeFunction(variable4, variable5)
        Dim variable10 = 3UI
        'Stop
    End Sub
    Public Shared Function SomeFunction(ByVal variable7, _
                                        Optional ByVal variable8 = "d", _
                                        Optional ByVal variable9 = 4)
        SomeFunction = variable8
        'Stop
    End Function
End Class
Download Broncode

Visual Studio : When entering breakmode ( for instance by a 'Stop' statement or by setting a breakpoint ) you can use the "Locals" debugtool to inspect the types ( dynamic and static ) of the variables.


Static type of variables 1, 2, 3, 5, 6, 7, 8 and 9 is Object. Variables 4 and 10 have static types Char and UInteger.

The dynamic type ( at runtime, after they've been assigned a value ) of variables 1 and 9 is 'Integer'. Variables 2, 5, 6 and 8 have dynamic type 'String', variable 3 has dynamic type 'Short', variables 4 and 7 have dynamic type 'Char' and variable 10 has dynamic type 'UInteger'.





This version ( published on 2008-06-24 ) is printed from http://www.studyvb.com, visit the website for more recent information.

Updated On : 2008-04-08

Download Broncode

Published On : 2008-06-24

Local Type Inference

Vorig Onderwerp

Partial Methods

|

Object Initializers

Volgend Onderwerp

New in Visual Basic 2008 - 9.0

Introduction to Visual Basic

Volgend Onderwerp
Nederlands  Nederlands

Add to favorites (IE).