tags:

views:

345

answers:

3

I wrote this - very simple - function, and then wondered does VB have some pre-built functionality to do this, but couldn't find anything specific.

Private Shared Function MakeArray(Of T)(ByVal ParamArray args() As T) As T()
    Return args
End Function

Not so much to be used like

Dim someNames() as string = MakeArray("Hans", "Luke", "Lia")

Because this can be done with

Dim someNames() as string = {"Hans", "Luke", "Lia"}

But more like

public sub PrintNames(names() as string)
   // print each name
End Sub

PrintNames(MakeArray("Hans", "Luke", "Lia"))

Any ideas?

+2  A: 

Any reason not to do:

Dim someNames() as string = New String()("Han", "Luke", "Leia")

The only difference is type inference, as far as I can tell.

I've just checked, and VB 9 has implicitly typed arrays too:

Dim someNames() as string = { "Han", "Luke", "Leia" }

(This wouldn't work in VB 8 as far as I know, but the explicit version would. The implicit version is necessary for anonymous types, which are also new to VB 9.)

Jon Skeet
Excellent, first example is exactly what I was looking for. Thanks.
Binary Worrier
+3  A: 
Dim somenames() As String = {"hello", "world"}
Mehrdad Afshari
A: 
PrintNames(New String(){"Hans", "Luke", "Lia"})
Joe