1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Passing Array to Sub

Discussion in 'Software Development' started by DavidWalsh, Jan 27, 2006.

Thread Status:
Not open for further replies.
Advertisement
  1. DavidWalsh

    DavidWalsh Thread Starter

    Joined:
    Nov 18, 2000
    Messages:
    97
    I want to be able to run a sub on different arrays that will be dynamic. How can I do this so that the original array is modified.

    Thanks for any help.
     
  2. OBP

    OBP Trusted Advisor

    Joined:
    Mar 8, 2005
    Messages:
    18,834
    What language?
     
  3. jiml8

    jiml8 Guest

    Joined:
    Jul 2, 2005
    Messages:
    2,634
    You need to pass the array by reference, not by value. Usually this is the default anyway for most languages, but the syntax depends on the language.
     
  4. DavidWalsh

    DavidWalsh Thread Starter

    Joined:
    Nov 18, 2000
    Messages:
    97
    Sorry, yes, should have said. VB6.
     
  5. cristobal03

    cristobal03

    Joined:
    Aug 5, 2005
    Messages:
    3,072
    You can pass arrays ByRef, but I believe you can return arrays ByVal (at least, you can in VBA).

    Code:
    ' calling procedure
    Sub SomeCaller()
      Dim vaByRefArray()  As Variant
      Dim vaByValArray()  As Variant
      Dim i               As Integer
    
      ReDim vaByRefArray(19)
    
      Call ByRefRoutine vaByRefArray
      vaByValArray = ByValRoutine()
    
      For i = 0 To UBound(vaByRefArray)
        Debug.Print vaByRefArray(i)
      Next i
    
      For i = 0 To UBound(vaByValArray)
        Debug.Print vaByRefArray(i)
      Next i
    End Sub
    
    
    ======
    
    Sub ByRefRoutine(ByRef MyArray() As Variant)
      Dim i As Integer
    
      For i = 0 To UBound(MyArray)
        MyArray(i) = i ^ 2
      Next i
    End Sub
    
    
    ======
    
    Function ByValRoutine() As Variant()
      Dim MyArray()  As Variant
      Dim i          As Integer
    
      ReDim MyArray(19)
    
      For i = 0 To UBound(MyArray)
        MyArray(i) = i / 2
      Next i
    
      ByValRoutine = MyArray
    End Function
    
    Like I said, that'll work in VBA, but I don't know about strict VB.

    chris.
     
  6. Ken Puls

    Ken Puls

    Joined:
    Jan 28, 2006
    Messages:
    118
    Hi there,

    Like Chris says, I don't believe you'll have an issue porting VBA code to VB, but if you want to be sure, Mark Rowlison has a great article on his site here: http://www.thecodenet.com/articles.php?id=6

    Hope it helps,
     
  7. RGregory

    RGregory

    Joined:
    Jul 27, 2005
    Messages:
    65
    I believe arrays are passed byref as default in VB6... You can always specify byRef in the parameters though, to ensure that you want to work on the actual variables and not copies.
     
  8. DavidWalsh

    DavidWalsh Thread Starter

    Joined:
    Nov 18, 2000
    Messages:
    97
    Thanks for the replies. I shall be having a look at implementing the methods that you you have suggested at sometime over the next few days. I'll let you know how I get on.
     
  9. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/437777