views:

916

answers:

3

Hello everybody,

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
        Debug.WriteLine(PassedArray(i))
    Next
End Sub

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

    SomeFunction(MyArray)
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)
{
    for($i=0;$i<count($array);$i++)
    {
         echo($array[$i]);
    }
}

function Test()
{
    SomeFunction(array("some", "array", "members")); // works for PHP
}

So to reiterate: Is there any 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.

PS.

SomeFunction({"some", "array", "member"}) ' this obviously gives a syntax error
+1  A: 

No; there is no such thing as a constant array in CLI; arrays are always mutable. Perhaps a ReadOnlyCollection<T> would be suitable?

In C# (so presumably similar in VB) you can do something like:

private readonly static ReadOnlyCollection<string> fixedStrings
    = new ReadOnlyCollection<string>(
        new string[] { "apple", "banana", "tomato", "orange" });

Which gives you a static (=shared) non-editable, re-usable collection. This works especially well if the method accepts IList<T>, IEnumerable<T>, etc (rather than an array, T[]).

Marc Gravell
+1 Perhaps an IEnumerable<T> would be better? The OP is only using the array to iterate the collection.
Andrew Hare
@Andrew; I was just editing to add that very suggestion (or IList<T> if the method requires indexer usage)
Marc Gravell
IEnumerable<T> is in my experience the best approach for method signatures, since you still have the option of calling into it with an array if you want to. If you're using .NET 3.5, you can still access elements at a certain position with the .ElementAt() extension method (although if you're doing this extensively you should probably pass an IList.
Ryan Brunner
+2  A: 

The closest you can do is :

SomeFunction(New String() {"some", "array", "members"})

This is actually identical in terms of objects created to what you posted. There aren't actually array literals in .NET, just helpers for initialization.

Ryan Brunner
Yes, I figured this out as soon Patrick McDonald posted his post. But I wanted to make sure and tested it before answering the question myself. Thanks everyone for answering.
Gert
I thought for some reason Dim s as String() = {"a", "b", "c"} wouldn't work and deleted my answer when I found it did, well done for working out what exactly the OP was asking. :)
Patrick McDonald
+1  A: 

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")
Ryan Brunner