Resetting Public Variables

  • Thread starter Gordon Bentley-Mix on news.microsoft.com
  • Start date
G

Gordon Bentley-Mix on news.microsoft.com

Whilst debugging a template this morning, I discovered some interesting
behaviour regarding variables declared as Public. While variables with
'local' scope - i.e. those declared using a Dim statement, either at the
module or procedure level - are unloaded from memory once code execution is
complete, it appears that variables declared as Public are not unloaded until
all documents based on the template containing the Public variable are closed.

From reviewing my code, I see that I've encountered this problem previously
without really being aware of what was happening. However, in this latest
effort, the cause suddenly became clear. Previously, I resolved this issue by
'resetting' these variables explicitly: erasing Public arrays; setting Public
'number-type' variables to "0" (zero); setting Public Boolean variables to
"False", setting Public string variables to "" (null); etc. However, in this
new project I have approximately 30 Public variables, and resetting them all,
while not an especially onerous task, isn't necessarily something I want to
do.

Can anyone shed any light on why Public variables have such 'persistance'? I
would expect that once code execution is complete, any variables would be
unloaded from memory regardless of how they've been declared.

And if this is "standard" behaviour (i.e. something that I'll just have to
cope with in spite of my expectations ;-P), then does anyone know an easy way
to dump Public variables out of memory - or at least reset them - without
having to do so explicitly? My big concern is that I have several similar
templates that use identically-named Public variables. I'm a bit worried that
if a user has 2 or more docs based on different (but similar) templates open
at once, there might be a conflict if these identically-named Public
variables are still hanging around in memory if the user switches between
docs.
--
Cheers!

Gordon Bentley-Mix
Word MVP

Please post all follow-ups to the newsgroup.

Read the original version of this post in the Office Discussion Groups - no
membership required!
 
P

Peter Jamieson

Can anyone shed any light on why Public variables have such
'persistance'? I
would expect that once code execution is complete, any variables would be
unloaded from memory regardless of how they've been declared.

I use VBA as little as I can, so you can obviously take what I say with
a pinch of salt. But no-one else seems to have replied, so...

One thing to bear in mind is that in the general case, the code itself
does not know when "code execution is complete". An example is the case
where you create a module to handle Application Events such as MailMerge
events. In that case you typically create a class module, create an
instance of the class and assign Word.Application to its App property.
You could argue that code execution is complete at that point. But you
haven't destroyed the class module, and then some pesky mailmerge starts
and all of a sudden Word starts using code in the class module.

Now I have to assume that you /do/ know when your code is complete. But
Perhaps sticking your variable definitions in a Class Module that you
instantiate at the beginning and destroy at the end would get rid of
those variables. However, you would have to modify your references to
them, e.g. if you have a class called Class1 and you use

Set x = new Class1

at the beginning of code execution and

Set x = Nothing

at the end, then you need to prepend all instances of your public
variables in that class by "x."


Peter Jamieson

http://tips.pjmsn.me.uk
Visit Londinium at http://www.ralphwatson.tv
 
G

Gordon Bentley-Mix

Thanks Peter. I'll take this into consideration. And my apologies for the
slow reply; I've been a bit busy. ;-P
 
P

Peter Jamieson

I've been a bit busy.

Hoped you were on hols :)

If you do get around to trying the class module approach I'd be
interested to see if it's a workable solution for you, and/or whether or
not other practical scope or variable lifetime issues get in the way.

Peter Jamieson

http://tips.pjmsn.me.uk
Visit Londinium at http://www.ralphwatson.tv
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top