Control Freak
Control Freak

Reputation: 13233

Passing array in function

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?

Upvotes: 3

Views: 31994

Answers (3)

aphoria
aphoria

Reputation: 20189

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

Upvotes: 1

Patrick Desjardins
Patrick Desjardins

Reputation: 140803

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.

Upvotes: 6

Chris Santiago
Chris Santiago

Reputation: 415

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

Upvotes: 2

Related Questions