开发者

Conversion of C# to VB.net List<T> has error

开发者 https://www.devze.com 2023-01-17 02:03 出处:网络
I\'m trying to convert some C# code to VB but I’m getting an error. What would be the correct VB syntax?

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

开发者_JAVA技巧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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号