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

Passing a whole array to a subroutine

Status
Not open for further replies.

jpspring

Programmer
Oct 6, 2000
25
GB
I am trying to pass a complete array to a subroutine, for example:
First Declare:
Dim MyArray() as string ' Dynamic array

Then if the subroutine is set ourt like this:
Public Sub MySub(SubArray() as string)

If i try and call it:
MySub(MyArray())

I get the error message that an array is expected.
Please help.
Thanks
 
Try
Code:
Dim MyArray () as string
Redim MyArray(0)
 [URL unfurl="true"]WWW.VBCompare.Com[/URL]
 
I must be going blind.
You said
"If i try and call it:
MySub(MyArray())
Either do
MySub MyArray()
Or
Call MySub(MyArray()).
Using parens () is a way to pass BYVAL so no changes can be amde. An array can not be passed BYVAL.
 
JohnYingling,

RE:


"Using parens () is a way to pass BYVAL so no changes can be amde"


John,
Where did you hear that using parens meant passing ByVal? That is simply not true (given your statment). Could you please explain your thinking behind that?
- Jeff Marler B-)
 
My answer was correct about removing the parantheses but perhaps BYVAL is the wrong term, more like by content but the effect is that any changes made to the BYREF parameter will not be reflected in the original argument. How VB dealt with turning (MyArray()) into an expression is beyond me.
This is from the VB 6 manual.
Code:
Passing Arguments By Reference
Passing arguments by reference gives the procedure access to the actual variable contents in its memory address location. As a result, the variable's value can be permanently changed by the procedure to which it is passed. Passing by reference is the default in Visual Basic.

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 (emphasis added). 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

Sub Foo(Bar As String)
   MsgBox Bar   'The value of Bar is the string "36".
End Sub
 
JohnYingling,
Yes, you did answer the question correctly that was originally posted in this thread. (Good job by the way:)!) . . . I just wanted to point out the ByVal issue to make sure that there were no mistakes or misinformation (on anyone's side) regarding the concept of what ByVal means. Thats all!
- Jeff Marler B-)
 
JohnYingling,
OK . . . one more thought on the ByVal/ByRef/Expression issue. Thanks for the info BTW . . . I did not know that VB handled the params differently when you removed the parens. Its always nice to learn something new! But either way, it is still passed ByRef.
From the way it sounds (readind the posted info), when Vb converts the parameter to an expression, it does some behind the scenes casting of variable types. It then passes a casted pointer to this data to the function. But sill ByRef. - Jeff Marler B-)
 
In COBOL lingo, they call this BY CONTENT as opposed to BY REFERENCE. A copy of the argument is passed BY REFERENCE. You mentioned "removing parens". Remember this was a SUB call so there normally are no parens. In a FUNCTION call you would add parens around an argument to pass a copy. In some situations regarding SUB (or FUNCTION calls with no result) calls you can get a SYNTAX error using parens so you must use a CALL where the list of arguments is surrounded by parens and the desired argument is itself surrounded by parens e.g. CALL foo((CSTR(variantString)). I don't use the "ByContent" much, if at all.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top