Lifetime of a module level variable

Rob.Mills

Registered User.
Local time
Today, 01:13
Joined
Aug 29, 2002
Messages
871
This is a rewording of a post I created a few hours ago.

I have some module level variables that seem to be losing their value when I don't want them to. I noticed it happening when I opened another form in dialog mode.

What would cause these variables to lose their value? I thought their lifetime is until the application is closed.
 
Right Click on the variable and hit "Add Watch" and then check "Break When Value changes"

This will let you know when it changes..

HTH
 
Well that's not exactly what I mean.

Basically, I've got a variable that's set to an object when the form opens and I want it to remain assigned to that object. I don't have any procedure that sets it to nothing. But sometimes when a prodecure is called that is suppose to use that variable I get an error saying it's not set to anything when I know it was before.
 
Is the variable dimensioned with

Dim or Public ?
If it is Dim'd inside of an event such as Form_Load its only good for the duration of the event. If it is Public and you are having troubles with keeping the value set then add a watch and see where your error is.

PS - you can edit your post rather than Creating Multiple Threads for the same question. The Admin will more than likely close one of your threads...
 
Last edited:
Rob,

Good advice to follow from Jerry and Sam (in the other post). I'll take a stab in the dark...

Are you doing something like this?
Code:
Option Explicit

'Declare and instantiate object.
   Public MyCalcObj As New CalcObj 
---------------------------------------------------------
Sub Form_Load

'Assign a value to a property.
   MyCalcObj.Value = 22

End sub

And are you hopefully not doing this anywhere else...
Code:
    Dim X as Long
'Not good...
    Set MyCalcObj = New CalcObj

   X  = MyCalObj.Value

The Set statement clears the object and resets any values you might have entered earlier.

Regards,
Tim
 
No, what I was doing was on open of the form set a module variable to an instance of a class I created. The only time I use the Set statement again is on close of the form to set it to nothing. But I have events that call certain methods from this class while the form is open and sometimes I get the error message that the variable is not set to anything even after I've called methods before which proved to me that it was set at one time. So I'm just trying to figure out if there's anything out of my control that would reset the value.
 
Rob.Mills said:
just trying to figure out if there's anything out of my control that would...

I know the feeling but also know there's always a reason and usually a solution.

I hope you don't feel like I'm beating you over the head with this, but, as Jerry and Sam said, the next step is to add a Watch Expression, if you already haven't. It's a tool expressly designed for situations just as you are describing. It will allow you to see when anything and everything happens to your object. Easy to use -- and once used it will potentially save you hours of time...

Also, don't declare and instantiate in the form's load event, if you are; do it in the Declarations section of the module, as I did above...

Or have you added a Watch Expression? What happened?

Regards,
Tim
 
If an error occurs and there is no error handling in place then the value of all variables is lost.
 
I'm probably being oblivious, but I would like to guarantee that a object stays alive for the application's entire lifetime, even if error handling has failed. The ideal place would be class module's built-in event Initialize and Terminate, but that would be really pointless because I'm already instantiating a object from a class (in an external library), and all public functions I have in standard modules are simply a wrapper to call the object's methods.

I tried to make it a static variable, but it didn't work out. Any suggestions?
 

Users who are viewing this thread

Back
Top Bottom