Re: passing array to function to pick up values

From: Birgit (albiwa.deletethis_at_moretodelete.xs4all.nl)
Date: 08/26/04


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
>
>



Relevant Pages

  • Re: Function CrossOver in VB.net
    ... Dim T as integer = dt.rows.count-1 ... Price1 has a variable arrival time, so that in order to get a ... you code to populate the source arrays won't work: ... Dim Price1 As String ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Conversion Problem
    ... the way to persists data is totally different so if you need to read legacy ... vb8 does not support arrays declared like arr1because when you ... dim arr1 ... I have then tried to Redim in an initialation so: ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Creating a UDF
    ... - You are currently requiring arrays of doubles. ... Dim nOpening As Integer ... ReDim lnOCAs Double ...
    (microsoft.public.excel.programming)
  • Re: transpose
    ... In vba, ... Sub Demo() ... Generally I would try to avoid using two arrays ... Dim BigNum As Integer, SmlNum As Integer ...
    (microsoft.public.excel.programming)
  • Re: Conversion Problem
    ... My overall intent is to convert an existing VB6 application into vb.net so I can still use multiple records I created in the new application. ... vb8 does not support arrays declared like arr1because when you try to do the Fileput it just gives an error saying only 2 dimentional arrays are supported - what a load of tat!!! ... dim arr1 ...
    (microsoft.public.dotnet.languages.vb)

Loading