Deborah's Developer MindScape






         Tips and Techniques for Web and .NET developers.

April 29, 2010

Collection Initializers

Filed under: C#,OOP,VB.NET @ 5:08 pm

One of the new features in VB 10.0 is collection initializers. Collection initializers allow you to initialize an array or list in a single line of code.

(C# has had collection initializers since C# 3.0. The examples here show both C# and VB for completeness.)

This example uses the customer class defined here and builds a list of customers using collection initializers.

In VB:

Dim custList As New List(Of Customer) From
            {New Customer() With
                  {.CustomerId = 1,
                    .FirstName = "Bilbo",
                    .LastName = "Baggins",
                    .EmailAddress = "bb@hob.me"},
            New Customer() With
                  {.CustomerId = 2,
                    .FirstName = "Frodo",
                    .LastName = "Baggins",
                    .EmailAddress = "fb@hob.me"},
            New Customer() With
                  {.CustomerId = 3,
                    .FirstName = "Samwise",
                    .LastName = "Gamgee",
                    .EmailAddress = "sg@hob.me"},
            New Customer() With
                  {.CustomerId = 4,
                    .FirstName = "Rosie",
                    .LastName = "Cotton",
                    .EmailAddress = "rc@hob.me"}}

In C#:

List<Customer> custList = new List<Customer>
        {new Customer()
              { CustomerId = 1,
                FirstName="Bilbo",
                LastName = "Baggins",
                EmailAddress = "bb@hob.me"},
        new Customer()
              { CustomerId = 2,
                FirstName="Frodo",
                LastName = "Baggins",
                EmailAddress = "fb@hob.me"},
        new Customer()
              { CustomerId = 3,
                FirstName="Samwise",
                LastName = "Gamgee",
                EmailAddress = "sg@hob.me"},
        new Customer()
              { CustomerId = 4,
                FirstName="Rosie",
                LastName = "Cotton",
                EmailAddress = "
rc@hob.me"}};

Notice how these two code snippets are very similar. VB even has the same curly braces!

The key differences are that VB requires the From keyword when initializing the list, the With keyword when initializing each customer in the list, and the "." in front of the property names.

Compare this VB example to the one shown for VB 9 (VS 2008) here.

Use this technique whenever you need to initialize a list or array of objects.

Enjoy!

2 Comments

  1.   Stan Schultes — May 26, 2010 @ 10:21 pm    Reply

    Deborah –

    Note that you can dramatically shorten the data initialization syntax in VB to this:

    Dim list As New List(Of Customer) From
    {
    {1, “Bilbo”, “Baggins”, “bb@hob.me”},
    {2, “Frodo”, “Baggins”, “fb@hob.me”},
    {3, “Samwise”, “Gamgee”, “sg@hob.me”},
    {4, “Rosie”, “Cotton”, “rc@hob.me”}
    }

    With the addition of an Add extension method for List(Of Customer). Be sure to place the extension method in a Module:


    Sub Add(ByVal list As List(Of Customer),
    ByVal CustomerID As Integer,
    ByVal FirstName As String,
    ByVal LastName As String,
    ByVal EmailAddress As String)

    list.Add(New Customer With
    {
    .CustomerId = CustomerID,
    .FirstName = FirstName,
    .LastName = LastName,
    .EmailAddress = EmailAddress
    })
    End Sub

    Thanks for sharing!
    Stan

  2.   DeborahK — May 27, 2010 @ 10:21 am    Reply

    Hi Stan –

    Thanks for your post!

RSS feed for comments on this post. TrackBack URI

Leave a comment

© 2023 Deborah's Developer MindScape   Provided by WPMU DEV -The WordPress Experts   Hosted by Microsoft MVPs