+ Reply to Thread
Results 1 to 12 of 12

Understanding Class Variable Lifetimes in relation to an add-in

  1. #1
    Forum Contributor
    Join Date
    05-27-2014
    Location
    Belfast, Northern Ireland
    MS-Off Ver
    2007 & 2010
    Posts
    273

    Understanding Class Variable Lifetimes in relation to an add-in

    Hi all.

    I have an add-in that currently retrieves user settings from an external file and stores them in a worksheet (within the add-in) for later use. It works but seems like a cumbersome way of doing it.

    I'm expanding my use of Class Modules and need somebody to help me clarify something.

    I've written a Class that can store the details of a setting (name, value, default). Then I've written a class which will serve as a collection of those settings.

    What is the lifetime of the classes?

    I get that lifetime and scope are closely related, but they're not the same. In the example of a procedure level "Static" vs "Dim", the scope is the same, that it can only be used in the procedure it was defined, but their lifetime is different. The lifetime of Dim will end when the procedure ends, the lifetime of Static will end when the macro ends, which means it will keep it's value between calls to the procedure it is defined in.

    My add-in is controlled by a Ribbonx Ribbon. There are various "tools" in the add-in that use lots of the same procedures as each other. When a tool is launched from the ribbon by the user, the selected macro executes, it accesses the values that were saved to the settings worksheet when the add-in loaded, it does it's thing, then it ends.

    Any other variable would be destroyed once the macro ends so each tool needs to read in the settings again upon launch. But because the add-in is still loaded, are my class variables still filled with data? If I was to replace the bit where a macro accesses the settings worksheet with accessing a class variable, would it work?

    What I want to do is get rid of the need to keep a worksheet holding the settings while the add-in is loaded. I want to import from the external file straight to the collection and then have that collection available at all times to any module (add-in "tool") that needs to use it.

    Not entirely sure I'm making myself clear on this one. I'm happy to clarify if you can help.

    Cheers.
    Last edited by kadeo; 07-28-2015 at 08:10 AM.
    Please click *Add Reputation if I've helped

  2. #2
    Forum Contributor
    Join Date
    05-27-2014
    Location
    Belfast, Northern Ireland
    MS-Off Ver
    2007 & 2010
    Posts
    273

    Re: Understanding Class Variable Lifetimes in relation to an add-in

    Can anyone shed some light on this?

  3. #3
    Forum Contributor
    Join Date
    05-27-2014
    Location
    Belfast, Northern Ireland
    MS-Off Ver
    2007 & 2010
    Posts
    273

    Re: Understanding Class Variable Lifetimes in relation to an add-in

    Anyone...?

  4. #4
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,480

    Re: Understanding Class Variable Lifetimes in relation to an add-in

    If you declare the objects at the top of a module, either as private or public, they will remain in memory until either you destory the instances in code or all variables are cleared because you Reset via VBE or code used the End command.

    As you appear to be reading from an external file you could always check, via code, that your objects have been created and loaded. If not then reload as required.

    So you can make use of the IsNothing keyword and then have a property within the class to confirm file has been read
    Cheers
    Andy
    www.andypope.info

  5. #5
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Understanding Class Variable Lifetimes in relation to an add-in

    An object (initialized class) will keep its values whilst ever something holds a reference to it. When all references to the object, goes out of scope, the lifetime of the object is over, something like:
    Please Login or Register  to view this content.
    The module level and static will keep their values unless explicitly set to nothing or the project is reset

  6. #6
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Understanding Class Variable Lifetimes in relation to an add-in

    What Andy said, if you were feeling really fancy, you could do what Andy suggests and alter the VB_PredeclaredId = True Class attribute so that it is self instantiating (like how userforms behave)

  7. #7
    Forum Contributor
    Join Date
    05-27-2014
    Location
    Belfast, Northern Ireland
    MS-Off Ver
    2007 & 2010
    Posts
    273

    Re: Understanding Class Variable Lifetimes in relation to an add-in

    Quote Originally Posted by Andy Pope View Post
    ...they will remain in memory until either you destory the instances in code or all variables are cleared because you Reset via VBE or code used the End command...
    That's what I keep reading "until all instances are destroyed" but I'm not sure how that applies to a Ribbon.

    The only code that references the class modules is the code in the add-in. Only specific parts of the code are run at the user's command from buttons etc on the Ribbon.

    Once a "tool" has finished executing, there is no other code running until the user interacts with the Ribbon again, so as far as I can tell, once a tool's code has finished executing there should be no more references to the class so it should be destroyed. So does this mean that while the Ribbon is active, the class remains in memory? I don't get it because the Ribbon itself has no code, no references to Class Modules, just callbacks.

    P.S. Andy, your Ribbon Designer has been invaluable to me during this project!!!! Excellent idea to produce an Excel Add-in to do this instead of a stand alone application that I wouldn't be able to install at work.

  8. #8
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Understanding Class Variable Lifetimes in relation to an add-in

    If you initialize it at module level, it will stay until the project is reset

  9. #9
    Forum Contributor
    Join Date
    05-27-2014
    Location
    Belfast, Northern Ireland
    MS-Off Ver
    2007 & 2010
    Posts
    273

    Re: Understanding Class Variable Lifetimes in relation to an add-in

    Quote Originally Posted by Kyle123 View Post
    ...The module level and static will keep their values unless explicitly set to nothing or the project is reset
    Quote Originally Posted by Kyle123 View Post
    If you initialize it at module level, it will stay until the project is reset
    Unless the project is reset? So, the project starts and Workbook_Open runs (which calls a sub in a Standard Code Module) instantiating class objects. So regardless of what the ribbon is doing, until the project is closed again (or I specifically destroy them), the objects remain in memory? If I've got that right, that seems to make sense.
    Last edited by kadeo; 08-06-2015 at 10:14 AM.

  10. #10
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Understanding Class Variable Lifetimes in relation to an add-in

    Yep AFAIK, though I've never done any ribbon development, but that's certainly how it works everywhere else - give it a whirl

  11. #11
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,480

    Re: Understanding Class Variable Lifetimes in relation to an add-in

    Glad you like it

  12. #12
    Forum Contributor
    Join Date
    05-27-2014
    Location
    Belfast, Northern Ireland
    MS-Off Ver
    2007 & 2010
    Posts
    273

    Re: Understanding Class Variable Lifetimes in relation to an add-in

    Thanks Andy and Kyle. That's definitely cleared up my understanding so I'll mark this as solved. Cheers guys.

+ 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. Understanding Variable implementation.
    By TheScott in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-08-2015, 06:11 AM
  2. Replies: 1
    Last Post: 02-28-2012, 01:51 PM
  3. Understanding Class Modules
    By MartinShort in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-25-2008, 03:52 AM
  4. Setting a Custom Class as a Global variable
    By Xiazer in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-31-2006, 06:55 AM
  5. [SOLVED] UNDERSTANDING VARIABLE SCOPE
    By -JEFF- in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 08-24-2005, 06:05 PM
  6. 180 day lifetimes in excel
    By corsair in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-09-2005, 04:00 PM

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