|
-
Jan 14th, 2004, 07:34 PM
#1
Thread Starter
Junior Member
Inconsistency passing arguments to subroutines
It seems that when an array is passed to a subroutine and changes made there to array elements, the result in the calling code is the same if the array is passed ByVal as when passed ByRef.
This means a different outcome results in the calling code from, say, an integer variable and an integer array with only one element being passed ByVal to a subroutine. Take the following code:
Dim arg(0) as integer
mySub(arg)
Sub mySub(ByVal aa(0) as integer)
aa(0) = 2
End Sub
Back in the calling code, arg(0) = 2 despite the array arg being passed ByVal. If an integer variable had been used instead, the value of the variable in the calling code would remain at zero because it was passed ByVal.
Seems inconsistent?
-
Jan 14th, 2004, 07:55 PM
#2
Arrays (from what I can work out) are byref regardless of if you use byval. This caught me out as well. But as soon as you redim it (even preserve) it creates a new array.
For example, you pass an array byref (like it makes a difference), add an extra value (redim ubound +1 FIRST) and the passed argument array hasnt changed. It was a bug that took me AGES to work out.
-
Jan 15th, 2004, 04:19 AM
#3
Sleep mode
Yeah , it's passed byref . If you want to work with safe , then try ArrayList . It's much more consistent and gives good performance and overall it's iteratable .
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|