Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing an array into a function.

Status
Not open for further replies.

todw17

Programmer
Apr 2, 2001
2
US
I want to pass in an array to a function. I have a select statement in which an array is selected based on the value of a counter. I wanted to move out a lot of the redundant code into a new function.

the basic setup is as follows:
Function selectArray(counter)
Select Case counter
Case 0
printArray(AlphaArray())
Case 1
printArray(BetaArray())
Case 2
printArray(GammaArray())
End Select
End Function

If I put all of the "printArray" code inside the Case statements, it works fine. It seemed much nicer to have a seperate function outside the selectArray function.

I'm not sure how to handle this inside the function that recieves the array as a parameter. I keep getting "Type Mismatch" errors. I'm sure this isn't too hard, however I just started using VB, so its all hard for me :).
If someone could provide some syntax I would appreciate it.

Thanks.

** variable and function names have been changed to protect the innocent. :)

 
I'm not quite sure what you're trying to do, but the way to pass an array to a function is to pass it as an object.

HTH
 
There are two basic ways to pass an array to a function.

The first is by using a variant, the second is by using a special array syntax.

The first looks like this:
function printArray(array as variant)
dim i
for i = lbound(array) to ubound(array)
debug.print array(i)
next
end function

The second looks like this:
function printArray(array() as string)
dim i
for i = lbound(array) to ubound(array)
debug.print array(i)
next
end function
 
Passing and Using arrays in Functions or Subroutines is a two step process: first you have to Define the Function/Subroutine:
Private Sub PrintArray( aArray() as String). Remark the use of parentheses for the Array in this definition. If you know the variable type (e.g. String) then always include it in the definition, otherwise you'll end up using variants, which are not too good for performance. Within this definition you may want to use LBound or/and UBound functions to fetch the size of the Bounds, as Swany does is his/her sample code.

Second you have to use or Call the Function/Subroutine. In this particular case, where a Sub has been defined, you can use either Call PrintArray (GammaArray) or PrintArray GammaArray. No parentheses are needed for the Array, because GammaArray is supposed to have been defined in your calling program. _________________________________
In theory, there is no difference between theory and practice. In practice, there is. [attributed to Yogi Berra]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top