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
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.
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
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..
Re: Clear all variables? Please??
Quote:
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
1 Attachment(s)
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:
Option Explicit
Dim mlLongVar As Long
Sub ProcOne()
mlLongVar = 10
MsgBox mlLongVar
mlLongVar = Empty 'Reset to nothing
End Sub
Sub ProcTwo()
MsgBox mlLongVar
End Sub
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
Re: Clear all variables? Please??
Have a read on collections. You may find what you want there... Perhaps.
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.. :D
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