+ Reply to Thread
Results 1 to 6 of 6

scope of objects in a collection

  1. #1
    Forum Contributor
    Join Date
    02-13-2016
    Location
    CT USA
    MS-Off Ver
    office 365 subscription
    Posts
    178

    scope of objects in a collection

    Questions about an object's scope when involved with collections


    Lets assume I have a subroutine whose input argument is an empty collection.
    Within the subroutine an object is declared and instantiated and then added
    to the collection.

    When the subroutine returns to the calling program, are the objects within the
    collection still in the collection now found in the calling program?

    Assume that I did not declare the object being collected as having module scope
    but only local scope.

    In the old days, forms of global variables were not good practice. But I do not see
    how else to preserve the object being collected other than to make its scope at module level

    Please Login or Register  to view this content.
    Last edited by whburling; 06-03-2017 at 04:13 PM.

  2. #2
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: scope of objects in a collection

    are the objects within the
    collection still in the collection now found in the calling program
    Simple enough to test...

    Please Login or Register  to view this content.

    You will be left with hidden instances of Word and Excel running in the background but the objects themselves will continue to exist as long as there's a reference to them somewhere.
    Last edited by cytop; 06-03-2017 at 04:45 PM.

  3. #3
    Forum Contributor
    Join Date
    02-13-2016
    Location
    CT USA
    MS-Off Ver
    office 365 subscription
    Posts
    178

    Re: scope of objects in a collection

    wow.

    first....thank you for showing me how easy it is to test ideas. I dreaded having to make the objects with the lets and gets and all that. I loved how you used existing objects (which was an idea I just did not think of).

    I will try your code out this evening.

    what I am hearing is that "o" a variable pointing to an object is local but as soon as I "add" "o" to the collection, the object in the container is no longer referrred to by a local variable but it exists for some reason.

    So am i understanding that objects are not local or global to a module or global to a project or global to all projects
    but exist until a reference to them ceases to exist? Is it only the variables that might point to them that are local
    global?

    If i can understand this last point I am over the hill.

    thank you very very much for responding to me, cytop, in a clear manner that I could understand.

    bil

  4. #4
    Forum Contributor
    Join Date
    02-13-2016
    Location
    CT USA
    MS-Off Ver
    office 365 subscription
    Posts
    178

    Re: scope of objects in a collection

    i just ran your code with a tiny addition or setting object o to nothing after you created the excel application.

    do the objects within the collection stay around because, in reading the object into the collection, a copy
    is created for the object. so the original object might have died with the return of subroutine "y", but the copy remains....AND....it remains as the collection was created outside of the collection filling code (subroutine y). Is this
    explanation accurate?

    This is exciting....wow.....I owe a great deal to you, cytops and to exNitWit who helped me in previous postings. thank you
    so very much

  5. #5
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: scope of objects in a collection

    It's not a copy of the object, it's an additional reference.

    An object will remain in memory as long as there is a reference to it held somewhere- each time you create a reference to it, a reference counter is incremented, and each time a reference is removed, the counter is decremented. An object will not be destroyed before that counter reaches 0. When you add the object variable to the collection, you create a copy of that pointer, thereby incrementing the reference count to 2. So even though the O variable has procedural scope, when it is cleared at the end of the procedure, that only reduces the reference count back to 1, so the object itself remains in memory due to the additional reference being held.
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  6. #6
    Forum Contributor
    Join Date
    02-13-2016
    Location
    CT USA
    MS-Off Ver
    office 365 subscription
    Posts
    178

    Re: scope of objects in a collection

    wow wow wow.....
    I am so damn excited. I really really understood your explanation, xlNitWit. I thank both you and cytop for hanging in there with me. As a way of thanking the two of you, all I can give you are photos I have shot of simple things in life or poetry where I make the simple complex....If you want either, let me know and I will send either or both to you.

    Regardless...I am trying to say, thank you.

    bil

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. scope and lifetime of vba objects
    By whburling in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 05-07-2017, 03:41 PM
  2. Replies: 4
    Last Post: 10-30-2015, 01:30 PM
  3. [SOLVED] Performance of iteration through Words collection degrades as collection gets bigger
    By 6StringJazzer in forum Word Programming / VBA / Macros
    Replies: 7
    Last Post: 01-14-2014, 09:44 AM
  4. Replies: 2
    Last Post: 12-23-2011, 11:04 AM
  5. Can I preset what order objects go out of scope?
    By foxguy in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-19-2011, 08:01 PM
  6. Problems storing a Collection within a Collection. Is this possible?
    By Tarball in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-17-2007, 10:47 PM
  7. Passing Objects from Excel VBA collection to a VB6 DLL
    By Dan in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-15-2006, 06:35 PM

Tags for this Thread

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