Possible Duplicate:
Collection initialization syntax in Visual Basic 2008?
This does not compile.
Dim Tom As New List(Of String) = {"Tom", "Tom2"}
This does
Dim Tom As String() = {"Tom", "Tom2"}
IMO this features should be allowed for all collection types and not only arrays.
Because array constants can only be used in initializers. You cant use them when assigning a new value to the variable. You can only use it during the initialization of the variable.
The initializer for an array is a comma-separated list of constant expressions enclosed in braces ( { } ). The initializer is preceded by an equal sign ( = ). You do not need to initialize all elements in an array.
An initializer-list is ill-formed if the number of initializer-clauses exceeds the number of members or elements to initialize.
No. The array is not being created twice. It is being created once and then it is being populated.
You cannot do this in the current version of Visual Basic, but the next version in Visual Studio 2010 allows this syntax:
Dim Tom As List(Of String) = new List(Of String) From {"Tom", "Tom2"}
It uses the new From keyword.
C#, on the other hand, has included its collection initializer syntax in Visual Studio 2008. You can read about it Object and Collection Initializers (C# Programming Guide) (MSDN).
Microsoft agrees with you. It is supported starting in the next release of VB, VB 2010. See this question: Collection initialization syntax in VB 2008?.
MSDN: What's New in Visual Basic 2010?
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