Passing Array in Function

Tag: vbscript Author: lxg_67 Date: 2011-09-14

I have an array that is read like this

    MyArray(0)='test'
    MyArray(1)='test2'
    MyArray(2)='test3'

How do I pass this through a function?

    Function(MyArray(all_arrays)) 

What do i put for 'all_arrays'?

Best Answer

MyArray(0)='test'
MyArray(1)='test2
MyArray(2)='test3'

AcceptArray MyArray

Private Function AcceptArray(myArray())
'Code here
End Function

You look to want to pass a string before the array.

So, change the Function to :

Private Function AcceptArray(param1, myArray)
'Code here
'Don't forget to return value of string type.
End Function

And you call this function like this:

returnValue = AcceptArray("MyString", MyArray)

If you do not need to return a value you should use a Sub.

comments:

When i pass function ('test', MyArray() as String) i get an error
You use it with : AcceptArray MyArray. If you want to pass other parameter you need to add it. I'll update my answer.
sorry, i dont understand, it wont let me put As String there.. it causes an error.
The reference I found online has: Private Function AcceptArray(byval param1, byref myArray) try something like this out
I modified the snippet of code because I though you were coding in vb6 no in vbscript. You do not have to put the type of the variable with AS statement.

Other Answer1

Looks like you need to define you function as such:

Function <FunctionName>(byref <list Name>)

then when you call it in your code use

<FunctionName>(MyArray)

found here: http://www.herongyang.com/VBScript/Function-Procedure-Pass-Array-as-Argument.html

passing by referrence using just the array name allows you to pass in the entire array to the function

comments:

Im much more familiar with VB.net but the source I found looks simmilar enough. hope this helps
what if the function has other values? function( 'test' , MyArray() ) ?
just change the function definition accordingly. then call it in your code the same way. define it as <FunctionName>(byval str, bref alist) and then when you call it use <FunctionName>('test', MyArray)

Other Answer2

A simple example...

Dim MyArray(2)
MyArray(0) = "Test"
MyArray(1) = "Test2"
MyArray(2) = "Test3"

ProcessArray MyArray

' -------------------------------------
' ProcessArray
' -------------------------------------
Sub ProcessArray(ArrayToProcess())
  For i = 0 To 2
    WScript.Echo ArrayToProcess(i)
  Next
End Sub

comments:

Function parameters can not contain parenthesis. Arrays are passed as normal variables without ().
@AutomatedChaos Thanks for the downvote...but you're wrong. Did you actually try the code? I slightly modified it to make it easier for you to test (DIMmed the array and added a loop to print the values).
Furthermore, the accepted answer is virtually identical to mine, but you didn't downvote it?
I didn't test you code. Now I did and it is actually working code so I corrected the downvote. But you shouldn't use parenthesis inside function parameters; they are not necessary and it is confusing. In VBScript you can practically use parenthesis everywhere, but that doesn't mean you should. And you are right about the accepted answer, I should have threated it the same (I am not on a personal crusade, just wanted to do some quality stuff here).
@AutomatedChaos I appreciate you changing your vote and I'm sorry I seemed so defensive. You are correct that the () are not required, but I can find nothing that indicates you should not use them in this manner. The only reference to it that I could find is on the Sub Statement reference page. Even that page isn't clear. I could interpret it as use () when the argument is an array or the () is optional. I kind of like using them because it is a reminder that you're passing an array.