Advertisement

There's no such thing as a stupid question, but they're the easiest to answer.
Login
Search

Advertisement

Software Development Software Development
Search Search
Search for:
Tech Support Guy > > >

Passing Array to Sub


(!)

DavidWalsh's Avatar
DavidWalsh DavidWalsh is offline
Member with 87 posts.
THREAD STARTER
 
Join Date: Nov 2000
27-Jan-2006, 06:56 PM #1
Passing Array to Sub
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.
OBP's Avatar
OBP OBP is offline OBP is a Trusted Advisor with special permissions.
Computer Specs
Trusted Advisor with 18,538 posts.
 
Join Date: Mar 2005
Location: UK
28-Jan-2006, 05:38 PM #2
What language?
jiml8's Avatar
Member with 2,634 posts.
 
Join Date: Jul 2005
Experience: I've been at this for too long.
28-Jan-2006, 08:16 PM #3
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.
DavidWalsh's Avatar
DavidWalsh DavidWalsh is offline
Member with 87 posts.
THREAD STARTER
 
Join Date: Nov 2000
29-Jan-2006, 01:23 PM #4
Sorry, yes, should have said. VB6.
cristobal03's Avatar
Member with 3,072 posts.
 
Join Date: Aug 2005
Experience: Advanced
29-Jan-2006, 03:03 PM #5
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.
Ken Puls's Avatar
Ken Puls Ken Puls is offline
Member with 118 posts.
 
Join Date: Jan 2006
Location: Nanaimo, BC, Canada
Experience: Advanced
30-Jan-2006, 01:54 AM #6
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,
RGregory's Avatar
RGregory RGregory is offline
Member with 65 posts.
 
Join Date: Jul 2005
Experience: Advanced
30-Jan-2006, 04:20 PM #7
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.
DavidWalsh's Avatar
DavidWalsh DavidWalsh is offline
Member with 87 posts.
THREAD STARTER
 
Join Date: Nov 2000
30-Jan-2006, 07:06 PM #8
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.
As Seen On

BBC, Reader's Digest, PC Magazine, Today Show, Money Magazine
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.


(clock)
THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


WELCOME
You Are Using: Server ID
Trusted Website Back to the Top ↑