Results 1 to 10 of 10

Thread: Clear all variables? Please??

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2005
    Location
    Houston, TX
    Posts
    7

    Clear all variables? Please??

    I've been searching around for a way to clear all variables, and I've found some mentions of it, but the question is usually asked in efforts to fix another problem and a different/limited solution ends up being sufficient for the user.

    What I have learned is that variables declared at the module level, opposed to the procedure level, do in fact retain their values after nominal code completion, and I also know you can clear individual variables by setting them to nothing. What I don't know is how to clear all variables at once, regardless of how many there are and what they are named.

    As a workaround, I was thinking that I could just use a for each loop to do it one at a time, but I can't find that there is a collection object for variables... ?

    Thanks in advance to anyone who can help.

    -agl

  2. #2
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    Re: Clear all variables? Please??

    Quote Originally Posted by agleinmiller
    What I have learned is that variables declared at the module level, opposed to the procedure level, do in fact retain their values after nominal code completion
    The best thing to do is try and keep the declarations inside an object other than a module, or within declarations..

    Global, Public And Variables Dim'd in the declaration section of a module become available throughout the project.
    Danny

    Never Think Impossible

    If you find my answer helpful then please add to my reputation

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2005
    Location
    Houston, TX
    Posts
    7

    Re: Clear all variables? Please??


    thanks for your input.

    typically i would declare them at the procedure level, but in this case i have several subs within the module that need to share various variables, and i was trying to get around having to pass them explicitly within the procedure calls.

    is there a way to declare module level variables at the procedure level? might result in the same problem i guess...

    -agl

  4. #4
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    Re: Clear all variables? Please??

    you can declare module specific variables by declaring them as Private, therefore they will not be available outside of the module..

    If the module is used for a set of functions look into creating it as Class module, therefore all variables within this will be cleared when you terminate the class..
    Danny

    Never Think Impossible

    If you find my answer helpful then please add to my reputation

  5. #5

    Thread Starter
    New Member
    Join Date
    Aug 2005
    Location
    Houston, TX
    Posts
    7

    Re: Clear all variables? Please??

    you can declare module specific variables by declaring them as Private, therefore they will not be available outside of the module..
    it's my understanding that this is true when you make a private declaration at the module level only, which is actually the same as using a dim statement at the module level. if you use a public declaration at the module level, then the variable is available outside the project as well as throughout the module. as far as i know you can't use public or private declarations at the procedure level.

    to clarify, i want to declare variables such that they will be shared across multiple procedures within a single module, and i want some way to clear their values at the beginning and/or end of code execution.

    i've never used class modules, and i'm not familiar with how they work and how they compare to standard modules... guess i'll have to check them out.

    -agl

  6. #6
    Lively Member JustinLabenne's Avatar
    Join Date
    Jul 2005
    Location
    Ohio
    Posts
    64

    Re: Clear all variables? Please??

    If they are declared at Module level:

    Reset the values of them at the end of each procedure,

    VB Code:
    1. Option Explicit
    2.  
    3. Dim mlLongVar As Long
    4.  
    5. Sub ProcOne()
    6. mlLongVar = 10
    7. MsgBox mlLongVar
    8. mlLongVar = Empty 'Reset to nothing
    9. End Sub
    10.  
    11.  
    12. Sub ProcTwo()
    13. MsgBox mlLongVar
    14. End Sub
    Attached Files Attached Files
    Justin Labenne
    www.jlxl.net

  7. #7

    Thread Starter
    New Member
    Join Date
    Aug 2005
    Location
    Houston, TX
    Posts
    7

    Re: Clear all variables? Please??

    that works for one variable at a time, but not for all.

    i'm trying to avoid creating multiple places where i have to maintain a complete list of all my variables. call it lazy if you want... i prefer "effort efficient."

    for example, i'd like a single function (i.e., application.reset, .clearmemory, .clearallvariables, etc.), or if someone smarter than me knows how i could repeat a method for clearing a single variable (i.e., dim var, var = empty, etc.) using a for loop or something in a way that would be independent of how many variables have been declared, that would suffice. something like

    for each var in variablescollection
    var = empty
    next

    -agl

  8. #8
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: Clear all variables? Please??

    Have a read on collections. You may find what you want there... Perhaps.

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  9. #9
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    Re: Clear all variables? Please??

    Actually I've found a way of doing this now...

    Quite simply.. End

    however it will also stop all code execution.. but at least it will clear all variables as well..
    Danny

    Never Think Impossible

    If you find my answer helpful then please add to my reputation

  10. #10

    Thread Starter
    New Member
    Join Date
    Aug 2005
    Location
    Houston, TX
    Posts
    7

    RESOLVED: Clear all variables? Please??

    sweet. that works fine for my needs. i'll just put an 'end' before the 'end sub' of my main procedure and everything should be copasetic.

    -agl

Posting Permissions

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



Click Here to Expand Forum to Full Width