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?
Any reason not to do:
The only difference is type inference, as far as I can tell.
I’ve just checked, and VB 9 has implicitly typed arrays too:
(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.)