5

Possible Duplicate:
Collection initialization syntax in Visual Basic 2008?

This does not compile.

Dim Tom As New List(Of String) = {"Tom", "Tom2"}

This does

Dim Tom As String() = {"Tom", "Tom2"}

IMO this features should be allowed for all collection types and not only arrays.

Community
  • 1
  • 1
Tomasi
  • 2,517
  • 6
  • 31
  • 43
  • Can't be done in VB.NET. C# does have 'var k = new List {"1", "2", "3"};' but there's no direct equivalent in VB.NET. – Jason Evans Mar 01 '10 at 20:42

2 Answers2

4

You cannot do this in the current version of Visual Basic, but the next version in Visual Studio 2010 allows this syntax:

Dim Tom As List(Of String) = new List(Of String) From {"Tom", "Tom2"}

It uses the new From keyword.

C#, on the other hand, has included its collection initializer syntax in Visual Studio 2008. You can read about it Object and Collection Initializers (C# Programming Guide) (MSDN).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nick
  • 5,875
  • 1
  • 27
  • 38
3

Microsoft agrees with you. It is supported starting in the next release of VB, VB 2010. See this question: Collection initialization syntax in VB 2008?.

MSDN: What's New in Visual Basic 2010?

Community
  • 1
  • 1
Jim Counts
  • 12,535
  • 9
  • 45
  • 63