列表的内联初始化(Of MyClass)

时间:2015-06-25 21:49:26

标签: vb.net

Here有人询问有关内联初始化vb.net中的字符串列表的问题。我有一个我在VS 2013中创建的类的列表(它只包含2个属性,ID为Integer,Name为string)。有没有办法将内联初始化为某些默认值?

我试过

Dim _products As New List(Of Product) From {{ID = 1, Name = "Product 1"}}

但没有运气......

1 个答案:

答案 0 :(得分:2)

你快到了。您创建了列表(New List(Of Product)),但您还需要自己创建对象(New Product):

Dim _products As New List(Of Product) From {
    New Product With {.ID = 1, .Name = "Product 1"},
    New Product With {.ID = 2, .Name = "Product 2"},
    ...
}