Passing Arrays to functions

How can I pass arrays to functions?
HoseheadAsked:
Who is Participating?
 
eyaltilConnect With a Mentor Commented:
hi,
Please look at the example i e_mailed u

tnx,
eyaltil@bigfoot.com
0
 
clifABBCommented:
Declare your Function Like this:
Private Function Test(MyArray() As String) As Integer

End Function

And then call it like this:
  Dim TestArray(100) As String
  nRes = Test(TestArray())

0
 
HoseheadAuthor Commented:
I do that, but it gives a compile error when I call the function saying that sub or function isn't defined and highlights the array:
var = function(array())
and it highlights array.

0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
clifABBCommented:
This is the proper method, so I would suggest there is something wrong with your code.

May I see a copy of the function's declaration and the call?
0
 
HoseheadAuthor Commented:
Note, All the array's used in the call are defined as public and used in other forms.  The FFSave Function hopefully will read these in and save them to an Excel spreadsheet. The call is made from a form when the user click on a save button.

Private Sub SaveReport_Click()
Dim verifyok As Integer

verifyok = FFSave(ReportTitle.Value, 0, WsF, _
            MFGarray(), MFGtitles, ConGarray, _
            ConGtitles, TTGarray, TTGtitles)
'test verifyok for value, then print a message
end sub


<------------In a different module------->
Function FFSave(ByVal RTitle As String, _
                ByVal RType As Integer, _
                ByVal Verify As String, _
                Array1() As Integer, _
                Array2() As String, _
                Array3() As Integer, _
                Array4() As String, _
                Array5() As Integer, _
                Array6() As String) As Integer





End Function

0
 
HoseheadAuthor Commented:
Adjusted points to 70
0
 
clifABBCommented:
OK, I can see some of your problems.

In your call, you don't specify the empty parantheses for arrays 2 through 6.  It should look like this:

verifyok = FFSave(ReportTitle.Value, 0, WsF, _
            MFGarray(), MFGtitles(), ConGarray(), _
            ConGtitles(), TTGarray(), TTGtitles())


In your declare, personally I would be more comfortable to declare the function as public:

Public Function FFSave(ByVal RTitle As String,  'etc

Finally, you note that the function is in a seperate module.  I assume this means a bas module (as opposed to a form module).  If it's bas module, that's good.  If it's a form module, you should move it to a bas module.

I am going to resubmit this as an answer because I'm fairly confident that the first problem was what was causing the error.
0
 
eyaltilCommented:
hi,
there is a problem to send an array to a procedure/function because an array is a linked pointers list.

here is one solution:
build a module with a private variant parameter, a public sub that get the array and put it in this parameter and a public function that return this paramter.
now, before calling your function, "set" your array to the module parameter and then "get" it from the module in the function.

if u would like an example send me your e_mail to eyaltil@bigfoot.com
bye,
0
 
HoseheadAuthor Commented:
When the () are in the call it gives the error "Sub or Function not defined" and hightlights the first array in the call.
0
 
HoseheadAuthor Commented:
Adjusted points to 100
0
 
topolCommented:
Usually, API functions need a pointer to the array, in this case, you can pass the first item of the array, for example
result=Function(array(0))
0
 
yowkeeCommented:
Hosehead,

  Where do you declare those array? In a .bas module? Or declare as public in a form? In latter case, you must specify the form name in front of the array. It seem VB could not find your array since you state it show "sub or function isn't defined" in your first comment.

 
0
 
clifABBCommented:
topol:
You are absolutely right, except that Hosehead isn't trying to pass an array to an API call.

Yowkee:
I made that point in my "rejected" answer.

Hosehead:
Can you cut and paste here the declares for the arrays as well as the filename of the module in which they are declared.  Also, cut and paste the line of code that calls the function?

Finally, as a test, create a new VB project.  Place a command button on the form.  Add the following code to the form:
Option Explicit
Dim MyArray() As String

Function ArrayTest(TheArray() As String) As Integer
  Dim nCnt1 As Integer
 
  ReDim TheArray(10)
  For nCnt1 = 1 To 10
    TheArray(nCnt1) = Str$(nCnt1)
  Next nCnt1
End Function

Private Sub Command1_Click()
  Dim nRes As Integer
  Dim nCnt2 As Integer
 
  nRes = ArrayTest(MyArray())
  For nCnt2 = LBound(MyArray) To UBound(MyArray)
    Debug.Print nCnt2, MyArray(nCnt2)
  Next nCnt2
End Sub

I just tried it and it works.
0
 
HoseheadAuthor Commented:
Okay, I declare the arrays in a seperate module (not a form) and declare them as public, but I don't set their size.  I redim them in the form where each array is used, two arrays per form, so 3 forms.  It is as each form is exited that the arrays are assigned their value.  Then in the master form, the one that called the other three I have a save button and that button calls the function with the arrays as options.  The Function itself is declared in a module where I have all of my public procedures etc...  If I can do this an easier way I would love to know.  None of this is set in stone.
0
 
clifABBCommented:
What you are doing should work.  I can't understand why it doesn't.

Have you tried my test code?  Is there any difference from yours(other than the use of seperate modules)?
0
 
HoseheadAuthor Commented:
Yes, there is a difference.  I am storing data to the array before I pass it to the function, I have the array already at a set size, but other then that, it would be the having it in different modules.
0
 
clifABBCommented:
Hmmm.  Perplexing indeed.  My sample code works like this as well (without the redim in the function):

Private Sub Command1_Click()
  Dim nRes As Integer
  Dim nCnt2 As Integer
   
  ReDim MyArray(10)
  nRes = ArrayTest(MyArray())
  For nCnt2 = LBound(MyArray) To UBound(MyArray)
    Debug.Print nCnt2, MyArray(nCnt2)
  Next nCnt2
End Sub

and it even works when I specifically declare the array:
Dim MyArray(10) As String

I wonder if this is a bug?  I am using VB5/SP3, how about you?
0
 
HoseheadAuthor Commented:
VB for Excel 97
0
 
clifABBCommented:
I just tested my code under VB Excel97 and it still works.  I even separated it into different modules.

A definite poser, this is.
0
 
HoseheadAuthor Commented:
Well, I was hoping it wasn't my code, but i'll take your example and try to figure out what is up, thanks all!
0
 
clifABBCommented:
eyaltil:
Considering that, once this goes to paq, there may be people who pay (10%) to see your answer, are you willing to email the example you speak of to all who request it?

Post your example.
0
 
eyaltilCommented:
ok
here it comes:

FORM1:

Option Explicit

Private Sub Form_Load()
Dim pArray() As String

'...
'...
'...
'PASSING THE ARRAY TO THE MODULE
Set_mArray pArray

'CALLING FORM2
Form2.Show
End Sub


FORM2:
Option Explicit

Private Sub Form_Load()
Dim pArray As Variant

'GETTING THE ARRAY FROM THE MODULE
pArray = Get_mArray()
'...
'...
'...


End Sub



MODULE1:
Option Explicit

Private mArray As Variant

Public Sub Set_mArray(pArray As Variant)
mArray = pArray
End Sub

Public Function Get_mArray() As Variant
Get_mArray = mArray
End Function




ANY QUESTION?
E_MAIL: eyaltil@bigfoot.com
0
 
topolCommented:
Yeah, passing Variant instead of array is good, but slow idea.
You then can access it as usual on arrays.
0
 
HoseheadAuthor Commented:
How would I pass single array arguments to the Set_mArray function if I so desired to do so (laziness, I know it is bad coding...)
0
All Courses

From novice to tech pro — start learning today.