Re: passing array to function to pick up values
From: Birgit (albiwa.deletethis_at_moretodelete.xs4all.nl)
Date: 08/26/04
- Next message: MG: "Re: Remove Leading Spaces"
- Previous message: fdde: "Re: Remove Leading Spaces"
- In reply to: Jay Freedman: "Re: passing array to function to pick up values"
- Messages sorted by: [ date ] [ thread ]
Date: Thu, 26 Aug 2004 09:16:25 +0200
Hi Jay,
Thank you so much! I believe I will have to look at all my projects very
carefully because I have this situation a lot (functions being called like
subs). However, I never ran into problems before. But then I was also not
passing around arrays. I'll avoid it in the future in order to make the code
more stable.
I also thank the other contributors to my question. I will keep those tips
for future projects.
Birgit
"Jay Freedman" <jay.freedman@verizon.net> schreef in bericht
news:uE7NlUuiEHA.1336@TK2MSFTNGP15.phx.gbl...
> Hi Birgit,
>
> This is a little strange: The array comes back empty because you're
calling
> a function but not assigning it to anything. If you do this
>
> Dim bRes As Boolean
> bRes = MakeArray(varArr)
>
> then it all works. Likewise, if you change the declaration of MakeArray to
a
> Sub and call it with
>
> MakeArray varArr
>
> then it works. (There's a stupid rule in VBA that you must use parentheses
> in a function call but you cannot use them in a subroutine call, as if the
> interpreter can't tell the difference.)
>
> --
> Regards,
> Jay Freedman
> Microsoft Word MVP FAQ: http://word.mvps.org
>
> Birgit wrote:
> > Hi all,
> > I am trying to sort out the fijner points of passing arrays between
> > functions. I simply do not understand how this works. Of course I get
> > it all to work with a global array, which I can modify anytime, but
> > I'd rather learn to do it by passing arguments. I have already found
> > to use ByRef while digging in the archive.
> >
> > If I run the function UseArray I get as result:
> > , bla
> >
> > instead of
> > <some character> , bla
> >
> > what's wrong?
> >
> > code follows
> > -------
> > Function MakeArray(ByRef myArray As Variant)
> > Dim teller As Long
> >
> > For teller = 1 To 10
> > 'assign some random alphanumeric stuff to array
> > myArray(teller) = Chr(teller * 10)
> > Next teller
> > End Function
> >
> > Function UseArray()
> > Dim varArr As Variant
> > Dim i As Long
> >
> > 'give a large dimension here because can't redim preserve inside
> > other function
> > 'can be shrunk afterwards if I ever get this to work
> > 'this code is just a tryout for other stuff I'm doing in a
> > project where I don't know
> > 'how many items I pick up along the way.
> > ReDim varArr(100)
> > MakeArray (varArr)
> > For i = 1 To 10
> > Debug.Print varArr(i) & " , bla"
> > Next i
> > End Function
>
>
- Next message: MG: "Re: Remove Leading Spaces"
- Previous message: fdde: "Re: Remove Leading Spaces"
- In reply to: Jay Freedman: "Re: passing array to function to pick up values"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|