O'Reilly logo

Microsoft® Visual Basic® 2010 Developer’s Handbook by Klaus Löffelmann and Sarika Calla Purohit

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Initializing Collections

Visual Basic 2010 has a new and elegant option that lets you initialize collections with values during declaration. This is similar to what you learned in the section Pre-Allocating Values of Array Elements in Code. The syntax is slightly different though, as the following example shows:

'Collection initializers:
Dim germanDogNamesList As New ArrayList From {"Waldi", "Basko", "Hasso",
                                             "Wauzi", "Bello", "Minka"}

For each element that you specify in braces behind the From keyword, the compiler generates an Add command that adds that element to the collection, as shown in the following:

Dim VB$t_ref$S0 As New ArrayList VB$t_ref$S0.Add("Waldi") VB$t_ref$S0.Add("Basko") VB$t_ref$S0.Add("Hasso") VB$t_ref$S0.Add("Wauzi") VB$t_ref$S0.Add("Bello") ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required