Static variable not static enough ??? 
Author Message
 Static variable not static enough ???

Hello,

I've created a VB ActiveX DLL.  Within a procedure, I've used the
"STATIC" keyword to preserve a variable.

The procedure is called from other procedures within the DLL itself as
well as from the main program.  It seems that the static variable is
only kept within the context of one of these projects.

Below is a test program I made.  Module1 (at the bottom) calls the
"ClassModuleThatCallsCounter" and that calls the "TestStaticVariable"
procedure 10 times.  It counts as one would expect.  But then when the
control returns to the "Main" procedure, it calls the
"TestStaticVariable" and the "Counter" variable is back to 0.

What I need is for the "COUNTER" variable to keep its value ALWAYS.

Any ideas?

Thank you in advance for guidance/ideas!


=================
Project: "Project2"
Class Module:  "ClassModuleWithCounter", GlobalMultiUse
===============================================

Public Sub TestStaticVariable()
    Static Counter
    Counter = Counter + 1
    Debug.Print Counter;
End Sub

=================
Project: "Project2"
ClassModule: "ClassModuleThatCallsCounter", GlobalMultiUse
===============================================
Public Sub Looper()
    Dim ClassModuleWithCounter As New ClassModuleWithCounter
    Dim LoopCount
    For LoopCount = 1 To 10
        Call ClassModuleWithCounter.TestStaticVariable
    Next LoopCount
End Sub

=================
Project "Project1"
References: "Project2"
Module:  "Module1"
=================
Sub Main()

    Dim Looper

    Dim ClassModuleWithCounter As New ClassModuleWithCounter
    Dim ClassModuleThatCallsCounter As New ClassModuleThatCallsCounter

    Call ClassModuleThatCallsCounter.Looper

    Call ClassModuleWithCounter.TestStaticVariable

    Call ClassModuleThatCallsCounter.Looper

    Stop
End Sub



Sun, 09 Dec 2001 03:00:00 GMT  
 Static variable not static enough ???
Looks to me like you've got two objects.

Sub Main creates an instances of both classes. When Sub Main calls looper,
the looper procedure creates a second instance of the ClassModuleWithCounter
class. This one prints to the debug window as you have observed. This object
ceases to exist when looper exits. When control returns to Sub Main, Sub
Main calls the TestStaticVariable method through the first object created,
which never had the looper method called at all.

It's important to remember that each object has its own completely
independent set of variables. You may feel like there is one static variable
called Counter, but there is actually one Counter variable per object. 50
objects, 50 counters.

Welcome to objects! Have fun!

Dan Buskirk


Quote:
> Hello,

> I've created a VB ActiveX DLL.  Within a procedure, I've used the
> "STATIC" keyword to preserve a variable.

> The procedure is called from other procedures within the DLL itself as
> well as from the main program.  It seems that the static variable is
> only kept within the context of one of these projects.

> Below is a test program I made.  Module1 (at the bottom) calls the
> "ClassModuleThatCallsCounter" and that calls the "TestStaticVariable"
> procedure 10 times.  It counts as one would expect.  But then when the
> control returns to the "Main" procedure, it calls the
> "TestStaticVariable" and the "Counter" variable is back to 0.

> What I need is for the "COUNTER" variable to keep its value ALWAYS.

> Any ideas?

> Thank you in advance for guidance/ideas!


> =================
> Project: "Project2"
> Class Module:  "ClassModuleWithCounter", GlobalMultiUse
> ===============================================

> Public Sub TestStaticVariable()
>     Static Counter
>     Counter = Counter + 1
>     Debug.Print Counter;
> End Sub

> =================
> Project: "Project2"
> ClassModule: "ClassModuleThatCallsCounter", GlobalMultiUse
> ===============================================
> Public Sub Looper()
>     Dim ClassModuleWithCounter As New ClassModuleWithCounter
>     Dim LoopCount
>     For LoopCount = 1 To 10
>         Call ClassModuleWithCounter.TestStaticVariable
>     Next LoopCount
> End Sub

> =================
> Project "Project1"
> References: "Project2"
> Module:  "Module1"
> =================
> Sub Main()

>     Dim Looper

>     Dim ClassModuleWithCounter As New ClassModuleWithCounter
>     Dim ClassModuleThatCallsCounter As New ClassModuleThatCallsCounter

>     Call ClassModuleThatCallsCounter.Looper

>     Call ClassModuleWithCounter.TestStaticVariable

>     Call ClassModuleThatCallsCounter.Looper

>     Stop
> End Sub



Sun, 09 Dec 2001 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Static variables are not supported...

2. static variable not being retained???

3. How do I declare a static global variable ?

4. Static Class and Variable

5. Declaring variables as Static vs Private/Public -- XML

6. Static variables in VBS?

7. Static Variable (VB)

8. Static variables...when to use?

9. Too many local, non-static variables

10. "Permanently" static variables

11. static variables

 

 
Powered by phpBB® Forum Software