Question Define an array inside of the sub call parentheses?

Kayot

Well-known member
Joined
Mar 6, 2007
Messages
49
Programming Experience
3-5
Is there a way to define an array inside of the sub call parentheses?

Private Sub sub_name(byval sString, arrString)

sub_name("Some Text", array.{"","",""})

What I've been doing is passing a string and using a split on it to break it into an array in the Sub/Function, but I was wondering if there was a way to make the array inside the parentheses. I've looked through Google, but I'm guessing I'm not asking the right questions or it's not possible.
 
Use the () array notation on either the parameter name or the type;
items() As String
or
items As String()
The first is most common in variable declarations in VB, which a parameter definition basically is, while the latter is the only possible way for function methods return types as arrays.
 
I know I can use Dim items() as string or Dim items as string(), I'm just trying to avoid using Dim. Right now it looks like this:

Public Sub MoveDirectories(ByVal sOrgPath As String, ByVal sdestPath As String, ByVal sDirList As String())

End Sub

Dim sFileName3() = {"FileName.EXT", "FileName2.EXT"}
MoveFiles("Path1", "Path2", sFileName3)

I know all that I'm going to put into the array. I'm just curious as to if I can setup the array in the called subs parameters in a way that doesn't first require me to Dim it. Much like how I can put strings in directly.

Right now I'm thinking my only option is to make the array into a delimited string and use Split inside the function that is being called. If there's a way to built the array inside the call, that would be wonderful and avoid a step in the called function, however I've tried several different idea's and none of them work.

I'm basically making sure there isn't a very specific way to do it before I setup a class delimiter to take the problem off my hands.
 
Of course you don't need to Dim a variable in caller, that was also not what you asked about nor I replied to.
This is valid in the VS version you're using:
VB.NET:
MoveDirectories("this", "that", {"a", "b", "c"})
You can also declare the last parameter as ParamArray, whereto you pass any number of arguments, which is then considered a single array.
 
I'm sorry, I wasn't asking the right question. I'm updating my VS to 2010, I'm currently using 2008 which I can't get to use .NET 4.0. I only had 4.0 installed. Sorry about that.
 
In VB 2008 you have to specify more explicitly the array initialization:
VB.NET:
MoveDirectories("this", "that", New String() {"a", "b", "c"})
How to: Initialize an Array Variable
 
Back
Top