Return a string array from a function without initialising it first

Public Function Foo() as String()
Dim bar As String = {"bar1","bar2","bar3"}

Return bar
End Function

My situation is similar to the code sample above where I’m returning a string array from a function.

What I would like to do is just return the string array without having to declare a variable first and then return the variable.

Something like this, although this obviously doesn’t work:

Return {"bar1","bar2","bar3"}

Is it possible to do this, I can’t seem to find a method that works?


You could do this:

Public Function Foo() As String()
    Return New String() {"bar1", "bar2", "bar3"}
End Function

Source : Link , Question Author : Richard Lucas , Answer Author : Darin Dimitrov

Leave a Comment