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!