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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to pass an array to a procedure ?

Status
Not open for further replies.

thomascd

Programmer
Mar 20, 2002
29
0
0
GB
Hello,
I can't succed to pass an array by reference to a procedure and modify the values directly. Can you tell me what is wrong in my code ?

Sub main()
Dim Array1(2, 1) As Long
Modify (Array1())
End Sub

Sub Modify(Array2 As Long)
For I = 0 To 2
Array2(I, 0) = I
Array2(I, 1) = I + 1
Next I
End Sub




 
By heart (not sure if it's exaclty true):

Loose the brackets when passing the argument to a function and add them in the function prototype (exactly opposite of what you're doing now).
Greetings,
Rick
 
well, still getting an error in sub main, saying that argument must be byref. Do I need to use a temporary variable ?
 
Please post both the sub/function that calls the modify function and the modify function. I checked and it's indeed opposite of what you've posted, so it should work (or are you maybe using the argument in the function prototype byval which is not possible).
Greetings,
Rick
 
When "lazy" says to loose the brackets, he means ALL of them, e.g.:
[tab]Modify Array1

MichaelRed
m.red@att.net

Searching for employment in all the wrong places
 
You need to modify the signature of [tt]Sub Modify[/tt] slightly.
[tt]
Sub main()
Dim Array1(2, 1) As Long
Modify (Array1())
End Sub

Sub Modify(Array2() As Long)
For I = 0 To 2
Array2(I, 0) = I
Array2(I, 1) = I + 1
Next I
End Sub
[/tt]
Rest is OK.
 
One more thing. You also need to modify this line:
Modify (Array1())
To this:
Modify Array1()
Or this:
Call Modify (Array1())
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top