+ Reply to Thread
Results 1 to 9 of 9

scope and lifetime of vba objects

Hybrid View

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

    Re: scope and lifetime of vba objects

    Hi Leith!

    Ok....let me share my understanding as it is at the moment. If it disagrees with your view, then don't think I am dead set on defending my point of view. Just help me see more clearly.

    As far as I can see, VBA mixes up several concepts.

    Declaring....and scope.

    Declaring, to me, is an instruction to a VBA compiler to associate a variable with a datastructure. Data structures do not occupy memory. they are merely a scheme that states that if this variable actually existed, then use the scheme to store
    data of interest. Declaring indicates nothing about actual memory used. Merely the amount of memory and the way it is
    organized if it did exist. Dim A as long tells the compiler that if A is encountered, it needs x bytes and is read in a certain manner. Dim A as SpecialObject is no different.

    My interpretation of Chip Pearson explanations suggests that when the compiled code is run and encounters a statement with the variable indicated above as follows: A = ASpecialObject then the run time code (1) copies the data from the datastructure of ASpecialObject and (2) assigns physical memory to A that conforms to the needs of A (not ASpecialObject) and places data in A according to the datastructure assigned to A. In summary: if both data structures are identical (which is checked at compile time). then memory is assigned at run time in the proper manner while executing the above statement.

    now scope....(the ability to see a specific variable inside a procedure, or outside of a procedure but still within a module, or outside of a module but still within a project, or outside a project and seen by all workbooks)....is interestingly not assigned at time of assigning memory!!!!! but at compile time. I suspect the compiler looks for special key words (as in private or public) in a declaration and the location of a declaration and at compile time determines the scope of a variable.

    Thus in my original example, an object created in its own class module must be declared in my main program at the top of the module that my main procedure is written in ....and that defines its scope (I suspect).

    It is actually created in the subroutine that I mentioned but because of the location of the declaration, the compiler ensures it is seen global to all procedures within the module regardless of where memory is actually assigned to the variable.

    what do you think?

  2. #2
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,236

    Re: scope and lifetime of vba objects

    Hey,

    My understanding is when you "DIM" a variable, it creates room (memory space) for that variable and points to that space using the name you used in the DIM statement.

    Dim LastRow as Double
    sets aside memory, within the subroutine or function you put in it that is the size of a "Double" and points to that memory spot using the name "LastRow".

    When the subroutine hits an "END" it clears all the memory it was using for the Sub and gives back the memory and variables. This is called "Popped off the stack".

    If you declare a Global variable, it can be seen by all the subroutines "within its scope" and you don't need to declare them in the individual subs.

    Does that make sense to you???

    Short story... In early computer days, there was this guy who liked to write fast code. He would challenge me often to race. I would always declare the variables that I knew would be used the most, first! Then when my code ran it wouldn't need to look through the whole list of variable names to find the spot in memory that held the value. He didn't think it mattered the order of the Dim statements but the computer needs to keep this list and then find the memory location where they are stored. BTW - just declaring the most used variables first kept me winning for a long time.
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

+ 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. Lifetime value of a contract
    By ammartino44 in forum Excel General
    Replies: 8
    Last Post: 05-15-2014, 06:20 PM
  2. 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
  3. Lifetime and class modules
    By bettatronic in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-28-2007, 09:34 PM
  4. What is lifetime of public variable?
    By John Wirt in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-26-2006, 12:20 PM
  5. Scope and Lifetime confusion.
    By hanjohn@netspace.net.au in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-07-2005, 09:05 AM
  6. Lifetime of VBA variables
    By Alan Beban in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-02-2005, 02:06 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