Results 1 to 3 of 3

Thread: Inconsistency passing arguments to subroutines

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2004
    Location
    Australia
    Posts
    30

    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?

  2. #2
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780
    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.

  3. #3
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    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
  •  



Click Here to Expand Forum to Full Width