Visual Basic 2008 9.0 .NET Examples and Ebook

New in Visual Basic 2008 - 9.0

Introduction to Visual Basic

Volgend Onderwerp

Object Initializers

Vorig Onderwerp

Local Type Inference

|

Anonymous Types

Volgend Onderwerp
-


Public field and writable properties can be initialized in the object initializer using a With clause.
An object initializer is formed with keyword New followed by the identifier of the type being initialized.


Class Person
    Private m_Name As String
    Public Property Name() As String
        Get
            Name = m_Name
        End Get
        Set(ByVal value As String)
            m_Name = value
        End Set
    End Property
    Private m_Address As Address
    Public Property Address() As Address
        Get
            Address = m_Address
        End Get
        Set(ByVal value As Address)
            m_Address = value
        End Set
    End Property
End Class
Class Address
    Private m_Street As String
    Public Property Street() As String
        Get
            Street = m_Street
        End Get
        Set(ByVal value As String)
            m_Street = value
        End Set
    End Property
    Private m_City As String
    Public Property City() As String
        Get
            City = m_City
        End Get
        Set(ByVal value As String)
            m_City = value
        End Set
    End Property
    Private m_ZipCode As String
    Public Property ZipCode() As String
        Get
            ZipCode = m_ZipCode
        End Get
        Set(ByVal value As String)
            m_ZipCode = value
        End Set
    End Property
End Class
Class Counter
    Public Sub New(ByVal value As Integer)
        m_Value = value
    End Sub
    Private m_Value As Integer
    Public ReadOnly Property Value() As Integer
        Get
            Value = m_Value
        End Get
    End Property
    Private m_StepValue As Integer
    Public Property StepValue() As Integer
        Get
            StepValue = m_StepValue
        End Get
        Set(ByVal value As Integer)
            m_StepValue = value
        End Set
    End Property
    Public Sub Raise()
        m_Value += StepValue
    End Sub
End Class
Class Example
    Public Shared Sub Main()
        Dim person1 As Person = New Person() With {.Name = "John"}
        Console.WriteLine(person1.Name)
        '
        Dim person2 As Person = New Person()                                ' (1)
        With person2                                                        ' (1)
            .Name = "Paul"                                                  ' (1)
        End With                                                            ' (1)
        '
        Dim person3 As Person = New Person() _
                                With {.Name = "Jane", _
                                      .Address = New Address() _
                                                 With {.City = "New York"}} ' (2)
        Console.WriteLine(person3.Name)
        Console.WriteLine(person3.Address.City)
        '
        Dim counter1 As Counter = New Counter(5) With {.StepValue = 4}      ' (3)
        Console.WriteLine(counter1.Value)
        counter1.Raise()
        Console.WriteLine(counter1.Value)
        '
        Console.ReadLine()
    End Sub
End Class
Download Broncode

Output :

 John
 Jane
 New York
 5
 9

(1) The use of a With clause in the object initialize is identical to the instantiation of an object followed by a seperate With ... End With statementblock.

The constructor is executed before the properties ( or fields ) are initialized, even when a With clause is being used in an object initializer.

(2) Several properties can be initialized within a With clause. At least one property ( or field ) needs to be initialized within a With clause, but not all properties ( or fields ) need to be initialized.
It is possible to nest several initilizers.

(3) A implicit call to a non-default constructor ( with arguments ) can be followed by a With clause. For instance New Counter(5) With {.StepValue = 4}.





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

Download Broncode

Published On : 2008-06-24

Object Initializers

Vorig Onderwerp

Local Type Inference

|

Anonymous Types

Volgend Onderwerp

New in Visual Basic 2008 - 9.0

Introduction to Visual Basic

Volgend Onderwerp
Nederlands  Nederlands

Add to favorites (IE).