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 = ""},
            New Customer() With
                  {.CustomerId = 2,
                    .FirstName = "Frodo",
                    .LastName = "Baggins",
                    .EmailAddress = ""},
            New Customer() With
                  {.CustomerId = 3,
                    .FirstName = "Samwise",
                    .LastName = "Gamgee",
                    .EmailAddress = ""},
            New Customer() With
                  {.CustomerId = 4,
                    .FirstName = "Rosie",
                    .LastName = "Cotton",
                    .EmailAddress = ""}}

In C#:

List<Customer> custList = new List<Customer>
        {new Customer()
              { CustomerId = 1,
                LastName = "Baggins",
                EmailAddress = ""},
        new Customer()
              { CustomerId = 2,
                LastName = "Baggins",
                EmailAddress = ""},
        new Customer()
              { CustomerId = 3,
                LastName = "Gamgee",
                EmailAddress = ""},
        new Customer()
              { CustomerId = 4,
                LastName = "Cotton",
                EmailAddress = ""}};

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.



  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”, “”},
    {2, “Frodo”, “Baggins”, “”},
    {3, “Samwise”, “Gamgee”, “”},
    {4, “Rosie”, “Cotton”, “”}

    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!

  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

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