Visual Basic 2008 9.0 .NET Examples and Ebook

New in Visual Basic 2008 - 9.0

Introduction to Visual Basic

Volgend Onderwerp

Anonymous Types

Vorig Onderwerp

Object Initializers

|

Argument Passing

Volgend Onderwerp
Key Properties

Key Properties

When to Use Anonymous Types

When to Use Anonymous Types



It is possible to create objects of undefined types.

Our algorithmes can not refer to these types, because they lack a name ( identifier ), they are anonymous.
One has to use a With clause in the object initializer to define the property names and values.


Option Infer On
Option Strict On
Class Example1
    Public Shared Sub Main()
        Dim address1 = New With {.City = "New York", .Street = "Parklane"}
        Console.WriteLine(address1.City)
        Console.WriteLine(address1.Street)
        '
        Dim address2 = New With {.City = "London", .Street = "Oxford Street"}
        Console.WriteLine(address2.City)
        Console.WriteLine(address2.Street)
        '
        Dim address3 = New With {.City = "San Fransico", .Street = 10}
        Console.WriteLine(address3.City)
        Console.WriteLine(address3.Street)
        '
        Dim address4 = New With {.City = "Paris"}
        Console.WriteLine(address4.City)
        '
        Dim city As String = "Brussels"
        Dim address5 = New With {city}                                     ' (1)
        Console.WriteLine(address5.City)
        address5.City = "Amsterdam"
        Console.WriteLine(address5.City)
        '
        Dim someInstance As New SomeFirstClass
        Dim address6 = New With {someInstance.City, someInstance.Street()}
        Console.WriteLine(address6.City)
        Console.WriteLine(address6.Street)
        '
        Dim address7 = New With {someInstance.City, someInstance.Street(), _
                                 someInstance.Number}                      ' (2)
        Console.WriteLine(address7.City)
        Console.WriteLine(address7.Street)
        Console.WriteLine(address7.Number)
        '
        Dim address8 = New With {SomeSecondClass.City, SomeSecondClass.Street, _
                                 SomeSecondClass.Number}                   ' (3)
        Console.WriteLine(address8.City)
        Console.WriteLine(address8.Street)
        Console.WriteLine(address8.Number)
        '
        Console.WriteLine(address1.GetType().Equals(address2.GetType()))
        Console.WriteLine(address1.GetType().Equals(address3.GetType()))
        Console.WriteLine(address1.GetType().Equals(address4.GetType()))
        Console.WriteLine(address4.GetType().Equals(address5.GetType()))
        Console.WriteLine(address1.GetType().Equals(address6.GetType()))
        '
        Console.ReadLine()
    End Sub
End Class
Class SomeFirstClass
    Public Number As Integer
    Public ReadOnly Property City() As String
        Get
            City = "someCity"
        End Get
    End Property
    Public Function Street() As String
        Street = "someStreet"
    End Function
End Class
Class SomeSecondClass
    Public Shared Number As Integer
    Public Shared ReadOnly Property City() As String
        Get
            City = "someCity"
        End Get
    End Property
    Public Shared Function Street() As String
        Street = "someStreet"
    End Function
End Class
Download Broncode

Output :

 New York
 Parklane
 London
 Oxford Street
 San Fransico
 10
 Paris
 Brussels
 Amsterdam
 someCity
 someStreet
 someCity
 someStreet
 0
 someCity
 someStreet
 0
 True
 False
 False
 True
 True

The compiler uses type inference to derive the type for the properties, this leads to strongly typed properties. At least one property needs to be defined.
When more than one property is defined, all properties need to have a unique name ( identifier ).

(1) Variables can be used to define the property names and values of anonymous types.

(2)(3) Shared and instance fields, properties and function can be used to define the property names and values of anonymous types. The properties and functions used for this purpose, can not have any arguments.

The properties created in these anonymous types are writeable, except when the Key keyword is used.

The name of the anonymous types is chosen by the compiler, and should not be used within your code.

An anonymous type is derived directly from Object.

The variables used to assign the reference of the object of the anonymous type to, can not be declared with a type specifier, after all the name of this type is unknow in our code. Therefore local type inference is used to derive the type of that variables.

Several anonymous types are of the same type if they are defined in the same assembly, if the have the same properties ( with identical names and datatypes ), defined in the same order.


Key Properties


Key properties are readonly and are used within the Equals implemenation to define whether or not two instances ( of the same anonymous type ) are logically equal.


Class Example2
    Public Shared Sub Main()
        Dim counter1 = New With {.Value = 0, .StepValue = 1}
        Dim counter2 = New With {.Value = 0, .StepValue = 1}
        Console.WriteLine(counter1.Equals(counter1))                       ' (1)
        Console.WriteLine(counter1.Equals(counter2))                       ' (2)
        '
        Dim counter3 = New With {Key .Value = 0, .StepValue = 1}
        Dim counter4 = New With {Key .Value = 0, .StepValue = 2}
        Dim counter5 = New With {Key .Value = 10, .StepValue = 1}
        Console.WriteLine(counter3.Equals(counter4))                       ' (3)
        Console.WriteLine(counter3.Equals(counter5))
        'counter5.Value = 11                                               ' (4)
        '
        Console.WriteLine(counter1.ToString())                             ' (5)
        Console.WriteLine(counter5.ToString())
        '
        Console.ReadLine()
    End Sub
End Class
Download Broncode

Output :

 True
 False
 True
 False
 { Value = 0, StepValue = 1 }
 { Value = 10, StepValue = 1 }

(1)(2) An instance of an anonymous type without Key properties is only equal to itself.

(3) Thow instances of the same anonymous type are logically equal if the hold the same values for all Key properties.

(4) Key properties are readonly.

All anonymous types override the ToString (5), Equals and GetHasCode implementations.


Klik hier om terug naar boven te gaan.  Up



When to Use Anonymous Types


The possibilities with anonymous types are limited, they can only contain properties ( with simple getters and setter ). When other type of members are needed, anonymous types are not suitable.

Anonymous types are typically only used within a local implementation, because they have no name, they can not be used as argumenttypes. Object of anonymous types are therefore not really suitable as arguments.





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-10

Download Broncode

Published On : 2008-06-24

Anonymous Types

Vorig Onderwerp

Object Initializers

|

Argument Passing

Volgend Onderwerp

New in Visual Basic 2008 - 9.0

Introduction to Visual Basic

Volgend Onderwerp
Nederlands  Nederlands

Add to favorites (IE).