views:

92

answers:

1

I'm trying to convert some C# code to VB but I’m getting an error. What would be the correct VB syntax?

C#

return new List<string>   {"First Name", "Last Name", "First & Last Name", "None"};

VB

Return New List(Of String)() From {"First Name", "Last Name", "First & Last Name", "None"}

And how about would I convert this too? Dim list As New List(Of Country)() From { New Country() With { Key .Name = "Select Country", Key .Code = "0" } }

Thanks

+6  A: 

Collection initialization is supported in VB10 (part of Visual Studio 2010), but not in VB9 (VS 2008). The syntax you posted is correct for VB10.

Dim foos As New List(Of String)() From {"Foo", "Bar"}

In VB9, you would just need to handle it the old fashioned way

Dim foos as New List(of String)()
foos.Add("Foo")
foos.Add("Bar")

VB9 does support array initialization

Dim foos As String() = New String() {"Foo", "Bar"}

However, the array is not as functional as the List(of T), but if you do not need to add or remove elements, you can certainly use an array instead of the list.

Anthony Pegram
Matthieu
How about this case??? Dim list As New List(Of Country)() From { New Country() With { Key .Name = "Select Country", Key .Code = "0" } }