+ Reply to Thread
Results 1 to 3 of 3

Can I preset what order objects go out of scope?

  1. #1
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Can I preset what order objects go out of scope?

    I try to automate as much of my coding as possible so that I don't have to remember to do something. The easiest way I have discovered is to create objects with the code that I need to remember in the Class_Terminate().

    In this example I turn EnableEvents = False in a class object. The wrapper sub (EEOff()) does not create an object unless EnableEvents=True. When object oEE goes out of scope it automatically turns EnableEvents back on. So I don't have to worry that I might have forgotten to turn it back on.
    Please Login or Register  to view this content.
    Every now and then the objects need to go out of scope in a particular order. In my example, I might need o2 to execute it's Class_Terminate before oEE executes it's Class_Terminate. Unfortunately they go out of scope in the wrong order, so I have to specifically remember to "Set o2 = Nothing" before the sub ends, which is what I'm trying to avoid.
    Please Login or Register  to view this content.
    Any suggestions on how to accomplish what I want?
    Foxguy

    Remember to mark your questions [Solved] and rate the answer(s)
    Forum Rules are Here

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,532

    Re: Can I preset what order objects go out of scope?

    First, I don't know the answer, but I am just really interested that someone is actually doing OOP with VBA.

    If threading were available I would think that you could set something up for a thread to manage this and each object would queue up to get permission to terminate and just block until it got permission. But I don't know a way in VBA to get that to happen.

    (BTW is your example a toy example? Because I'm trying to figure out why you just don't declare oEE as a new Cls_EEOff to start with, but maybe you have a more complex situation where you need the late binding.)
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: Can I preset what order objects go out of scope?

    Quote Originally Posted by 6StringJazzer View Post
    (BTW is your example a toy example? Because I'm trying to figure out why you just don't declare oEE as a new Cls_EEOff to start with, but maybe you have a more complex situation where you need the late binding.)
    Although there are times when I want late binding, the main reason I don't declare it to start with is because I have a macro that creates the structure for all my subs automatically from a menu selection. So all my subs declare oEE but don't do anything with it and at the end of the sub it tests to see if oEE is nothing. Because I can't count on oEE going out of scope when I want it to, I have it automatically setting oEE = Nothing before the sub ends.
    Then I have another macro put in "Set oEE = EEOff()". I could have that macro put in "Dim oEE as New cls_EEOff" and "If Not oEE is Nothing Then ....." at the end of the sub, but it was just too much trouble when I created it to put the "If Not ...." at the bottom of the sub. Also by having a "Dim oEE" already in the sub, it helps remind me when I need to turn off Events.

    I'm not sure I explained that adequately. If not let me know and I'll try again.

+ 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