Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inline initialization of a List(Of MyClass)

Tags:

vb.net

Here a question was asked about inline initializing a list of strings in vb.net. I have a list of a class i created in VS 2013 (it just contains 2 properties, ID as Integer, Name as string). Is there a way to inline-initialize it to some default values?

I tried

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

but no luck...

like image 923
Wanderer Avatar asked Oct 28 '25 18:10

Wanderer


1 Answers

You're almost there. You created the list (New List(Of Product)), but you also need to create the objects themselves (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"},
    ...
}
like image 57
Heinzi Avatar answered Oct 31 '25 12:10

Heinzi