Gert Gert - 10 months ago 24 Question

Passing a constant array to a function in VB.NET

I know that you can easily pass an array to a function, like the code below shows

Private Sub SomeFunction(ByVal PassedArray() As String)
For i As Integer = 0 To PassedArray.Count - 1
End Sub

Public Sub Test()
Dim MyArray As String() = {"some", "array", "members"}

End Sub

But is there a way to pass a constant array to a function in VB.NET?

For instance in PHP, you could write:

function SomeFunction($array)

function Test()
SomeFunction(array("some", "array", "members")); // Works for PHP

So to reiterate: Is there a way to pass a constant array directly to a function in VB.NET? Is there any benefit in doing so? I imagine a few bytes of memory could be spared.


SomeFunction({"some", "array", "member"}) ' This obviously gives a syntax error


Another thing I just thought of that doesn't directly answer the question, but perhaps gets at the poster's intent - the ParamArray keyword. If you control the function you are calling into, this can make life a whole lot easier.

Public Function MyFunction(ByVal ParamArray p as String())
   ' p is a normal array in here
End Function

' This is a valid call
MyFunction(New String() {"a", "b", "c", "d"})

' So is this
MyFunction("a", "b", "c", "d")