+ Reply to Thread
Results 1 to 10 of 10

Assigning One Array To Another by reference

  1. #1
    Registered User
    Join Date
    01-08-2009
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    19

    Assigning One Array To Another by reference

    Hello,
    I'm trying to set up multiple "pointers" to the same dynamic array, but can't seem to figure out how.
    reading the MS Article on how to do this on http://msdn.microsoft.com/en-us/libr...ffice.10).aspx
    I was expecting to be able to change a(1) by changing b(1) on the below code snippet, but can't seem to get it to work
    The statement b=a seems to make an independent copy of a, rather than make b point to the same memory location of a
    This becomes apparent when the statement b(1)=.222 changes b but not a as per the "Watches" window

    Any ideas on how to set more than one pointer to the same dynamic array?
    Help would be much appreciated, thanks.

    Please Login or Register  to view this content.

  2. #2
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: Assigning One Array To Another by reference

    I'm intrigued - why would you want this?
    Good luck.

  3. #3
    Registered User
    Join Date
    01-08-2009
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    19

    Re: Assigning One Array To Another by reference

    I've got a tight loop where a functor needs to operate on an array which needs to be allocated and managed elsewhere.
    The functor needs to know the array, but it cannot receive it on an argument list in every call in the loop or the loop will be significantly slower.
    The standard solution is to construct the functor outside the loop passing this array as an argument so it has knowledge of it and can operate on it within the loop when it is called later.

  4. #4
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: Assigning One Array To Another by reference

    Other than a public variable, I don't know how you could do that in VBA. VarPtr won't work with an entire array, and I'm not sure I would recommend messing about with CopyMemory API calls anyway.

  5. #5
    Registered User
    Join Date
    01-08-2009
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    19

    Re: Assigning One Array To Another by reference

    Interesting, but what about the MS article?
    It is clearly wrong in its implementation instructions, but is it also wrong about this sort of thing even being possible?
    There's gotta be a syntax for this, it's pretty basic stuff no?

  6. #6
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Assigning One Array To Another by reference

    I don't think that article implies what you think it implies.

  7. #7
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: Assigning One Array To Another by reference

    I don't think this is basic stuff - I can't say I have ever had a need for it that could not be achieved by simply passing the array to a function, or using a public variable.

  8. #8
    Registered User
    Join Date
    01-08-2009
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    19

    Re: Assigning One Array To Another by reference

    From the Microsoft article:
    Assigning one array to another of the same type is quick because the first array is simply pointed to the memory location that stores the second array.
    Does this not mean that Microsoft think you can look at the same array using different "pointers"?

  9. #9
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Assigning One Array To Another by reference

    It's somewhat ambiguous, but the way I read it, it is referring only to initialising the second array. It doesn't mean any sort of subsequent connection. But, as OEG0 says, there are other ways to skin this cat.

  10. #10
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: Assigning One Array To Another by reference

    I think it's completely misleading and couldn't be more badly worded without a lot of effort.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1