Solved

Passing Arrays to functions

Posted on 1998-06-09
24
357 Views
Last Modified: 2010-05-03
How can I pass arrays to functions?
0
Comment
Question by:Hosehead
  • 10
  • 8
  • 3
  • +2
24 Comments
 
LVL 6

Expert Comment

by:clifABB
Comment Utility
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
 

Author Comment

by:Hosehead
Comment Utility
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
 
LVL 6

Expert Comment

by:clifABB
Comment Utility
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
 

Author Comment

by:Hosehead
Comment Utility
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
 

Author Comment

by:Hosehead
Comment Utility
Adjusted points to 70
0
 
LVL 6

Expert Comment

by:clifABB
Comment Utility
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
 

Expert Comment

by:eyaltil
Comment Utility
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
 

Author Comment

by:Hosehead
Comment Utility
When the () are in the call it gives the error "Sub or Function not defined" and hightlights the first array in the call.
0
 

Author Comment

by:Hosehead
Comment Utility
Adjusted points to 100
0
 

Expert Comment

by:topol
Comment Utility
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
 
LVL 4

Expert Comment

by:yowkee
Comment Utility
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
 
LVL 6

Expert Comment

by:clifABB
Comment Utility
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:Hosehead
Comment Utility
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
 
LVL 6

Expert Comment

by:clifABB
Comment Utility
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
 

Author Comment

by:Hosehead
Comment Utility
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
 
LVL 6

Expert Comment

by:clifABB
Comment Utility
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
 

Author Comment

by:Hosehead
Comment Utility
VB for Excel 97
0
 
LVL 6

Expert Comment

by:clifABB
Comment Utility
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
 

Author Comment

by:Hosehead
Comment Utility
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
 

Accepted Solution

by:
eyaltil earned 120 total points
Comment Utility
hi,
Please look at the example i e_mailed u

tnx,
eyaltil@bigfoot.com
0
 
LVL 6

Expert Comment

by:clifABB
Comment Utility
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
 

Expert Comment

by:eyaltil
Comment Utility
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
 

Expert Comment

by:topol
Comment Utility
Yeah, passing Variant instead of array is good, but slow idea.
You then can access it as usual on arrays.
0
 

Author Comment

by:Hosehead
Comment Utility
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

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now