Startsidan  ▸  Texter  ▸  Teknikblogg

Anders Hesselbom

Programmerare, skeptiker, sekulärhumanist, antirasist.
Författare till bok om C64 och senbliven lantis.
Röstar pirat.

Collection initializers

2009-11-08

Now I have had the opportunity to try out the new collection initializers of Visual Basic 10. This is good stuff and I am very happy about being able to do line breaks when I use this (just as C programmers can do whenever they want). Examine this simple example that consist of a ContactPerson class, and a generic collection that I initialize using the collection initializer.

Module Module1

    Public Class ContactPerson
        Public FirstName As String
        Public LastName As String
        Public EMail As String

        Public Overrides Function ToString() As String
            Return Me.FirstName & " " & Me.LastName & " " & Me.EMail
        End Function
    End Class

    Sub Main()

        Dim L As New List(Of ContactPerson) From {
         {New ContactPerson With {.FirstName = "Anders", .LastName = "Andersson", .EMail = "anders@some.thing"}},
         {New ContactPerson With {.FirstName = "Bertil", .LastName = "Bengtsson", .EMail = "bertil@some.thing"}},
         {New ContactPerson With {.FirstName = "Calle", .LastName = "Ceder", .EMail = "calle@some.thing"}}
         }

        For Each P As ContactPerson In L
            Console.WriteLine(P.ToString())
        Next

    End Sub

End Module

Note that the element type does not require any special constructor, I can provide a constructor and target that, but here I am using the object initializer that was introduced in Visual Basic 9.

Instead of the parenthesis that I would use to call the List constructor, I use the From keyword with a curly bracket, and each element is surrounded by curly brackets, separated by a comma. Finally, I have to use another set of curly brackets because I use the object initializer for each element, instead of a regular constructor.

If I adapt this code to use a constructor for each element, this is how it could look. This uses the new collection initializer and traditional constructors (and therefor less curly brackets).

Module Module1

    Public Class ContactPerson
        Public Sub New(ByVal FirstName As String, ByVal LastName As String, ByVal EMail As String)
            Me.FirstName = FirstName
            Me.LastName = LastName
            Me.EMail = EMail
        End Sub
        Public FirstName As String
        Public LastName As String
        Public EMail As String

        Public Overrides Function ToString() As String
            Return Me.FirstName & " " & Me.LastName & " " & Me.EMail
        End Function
    End Class

    Sub Main()

        Dim L As New List(Of ContactPerson) From {
         {New ContactPerson("Anders", "Andersson", "anders@some.thing")},
         {New ContactPerson("Bertil", "Bengtsson", "bertil@some.thing")},
         {New ContactPerson("Calle", "Ceder", "calle@some.thing")}
         }

        For Each P As ContactPerson In L
            Console.WriteLine(P.ToString())
        Next

    End Sub

End Module

Categories: VB.NET

2 responses to “Collection initializers”

  1. Hello from TechEd Berlin!

  2. Hello Jesper and BERLIN!

Leave a Reply

Your email address will not be published. Required fields are marked *



En kopp kaffe!

Bjud mig på en kopp kaffe (20:-) som tack för bra innehåll!

Bjud på en kopp kaffe!

Om...

Kontaktuppgifter, med mera, finns här.

Följ mig

Twitter Instagram
GitHub RSS

Public Service

Folkbildning om public service.

Hem   |   linktr.ee/hesselbom   |   winsoft.se   |   80tal.se   |   Filmtips