In c# I can initialize a List at creation time like
var list = new List<String>() {"string1", "string2"};
is there a similar thing in VB.Net? Currently I can do it like
Dim list As New List(Of String)
list.Add("string1")
list.Add("string2")
list.Add("string3")
but I want to avoid boring .Add lines
To add items to a ListBox, select the ListBox control and get to the properties window, for the properties of this control. Click the ellipses (...) button next to the Items property. This opens the String Collection Editor dialog box, where you can enter the values one at a line.
In visual basic, List is a generic type of collection so it will allow storing only strongly typed objects i.e. elements of same data type and the size of list will vary dynamically based on our application requirements like adding or removing elements from the list.
VB10 supports collection initializers. I believe your example would be:
Dim list As New List(Of String) From { "string1", "string2", "string3" }
MSDN has more information.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With