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

Array ByRef 2

Status
Not open for further replies.

johnaregan

Programmer
Mar 13, 2001
87
Hi
I'm trying to pass an array ByRef to a function in order to change the contents of the array, the array is created by:
ar1 = Split("A","B","C")

The function call is:
change(ar1)

and the function is:
Function(ByRef tmp)
...code to change value to "D","E","F"
End Function

but when I display the contents of the ar array back in the main code it is unaffected by the function call, instead of displaying D, E, F is displays A, B, C.

Any help would be appreciated


 
Hello, johnaregan.

From what you post, I can only think of the assignment orignally made to a1.

a1=Split("A","B","C") '_wrong_
Should be:
a1=Array("A","B","C")

/Or/

by chance you have a string s say,
s = "A,B,C"
a1 = Split(s,",")

Both get you the same a1 in substance. And you would be OK.

regards - tsuji
 
Nope. Take the parens off or add CALL
change ar1
OR
Call change(ar1)
OR
dummyreturn = Change(ar1)

Your "form" of "call" was as a SUB, not a function. Putting parens around a SUB Call argument tels VB to make a copy and pass the copy BYREF.
TRY IT. It is a common mistake.
Try this and see if you get the same error.
Call change((ar1))
 
Acute observation, John Yingling. I like your note.

regards - tsuji
 
JohnYingling
Thanks for the tip it works ok now.

 
Observation yes. I stepped in it in my own code a few times. It is a documented (buried) "feature" in VB (Using VB / Prog Guide / Part1 / Prog Fundamentals / Into to procs / Passing Arguments. I do not see in the VBScript docs.

"If you specify a data type for an argument passed by reference, you must pass a value of that type for the argument. You can work around this by passing an expression, rather than a data type, for an argument. Visual Basic evaluates an expression and passes it as the required type if it can.

The simplest way to turn a variable into an expression is to enclose it in parentheses. For example, to pass a variable declared as an integer to a procedure expecting a string as an argument, you would do the following:

Sub CallingProcedure()
Dim intX As Integer
intX = 12 * 3
Foo(intX)
End Sub

"
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top