VB9 and collection initializers

Roger Jennings asks about VB9 and collection initializers.  It’s a question I too have asked, and although I’m not certain it does appear there won’t be collection initializers for VB9 🙁

One thing I did want to point out though is in regards to the array initializers. Roger posted the code :

Dim Capitals = { _
New City With { _
.Name = “Antanarivo”, _
.Country = “Madagascar”, _
.Longitude = 47.4, _
.Lattitude = -18.6 }, _
New City With { _
.Name = “Belmopan”, _
.Country = “Belize”, _
.Longitude = -88.5, _
.Latitude = 17.1 }, _
New City With { _
.Name = “Monaco”, _
.Country = “Monaco”, _
.Longtitude = 7.2, _
.Latitude = 43.7 }, _
New City With { _
.Country = “Palau”,
.Name = “Koror”, _
.Longitude = 135, _
.Latitude = 8 } _


And goes on to say that “which should read Dim Capitals = New City(){ _. It’s clear that no one tested the code before publishing the update”

I don’t think that’s a fair comment as the document shows code that isn’t even implemented in Beta 1. (e.g operators on Nullable types). So it is clear the document is forward looking, and talking about how things will/should be.

So, given that, if we stop and analyze the syntax for array initializers it should be basically as presented. Perhaps one could argue the syntax should be Dim Capitals() = {.    It should also support the syntax you can use today in VS 2005, e.g: Dim ints() As Int32 = {1,2,3} , or as per the code Roger suggests Dim ints() As Int32 = New Int32(){1,2,3}

Okay so given the minimum it needs to support (current syntax), in VB9 we add anonymous types and inferred typing.  Inferred typing means we remove the “As XXX” part.  So this means the syntax Dim Capitals As City() becomes Dim Capitals and the Dim Capitals() As City syntax becomes  Dim Capitals().  Anonymous types means we can’t define the type name. So New City() { would become New() { _ , and given that the existing syntax doesn’t even require the New Int32(), the New() becomes superfluous because the set brackets { } define the array data.

IOW: the syntax as shown is as it should be. (IMO)


1 Comment so far

  1.   Roger Jennings on June 25th, 2007          


    As I said in the later post’s comments, you got me on the & operator.

    But I don’t agree with your analysis of the issues I raised about the sample code in the current “Overview of Visual Basic 9.0” page. There’s a detailed response at the bottom of http://oakleafblog.blogspot.com/2007/06/will-visual-basic-90-have-collection.html.