Static Variables in Classes 
Author Message
 Static Variables in Classes

OK, here's what I'm trying to do.

I want to declare a static variable in the declarations section of a class.
This is capable of being done in C++ and Java but I haven't quite figured
out how to do it in VB.

What I need is a variable that is available to ALL objects created from
this class, and when that variable changes, ALL objects see the new value
of that variable.

Any and all help is greatly appreciated,
-Brian

--
--------------------------------------------
Brian Orrell
Dallas, TX

fax: 214.522.1927



Mon, 04 Oct 1999 03:00:00 GMT  
 Static Variables in Classes

Perhaps I misread your question, but don't you merely want an ordinary
Private variable in the declarations section of your class, and then
ordinary Let/Get routines so that anything that has a reference to the
class can access it?

In order to have it accessible to "ALL objects created from
this class", you just have to pass each of those objects a reference to the
class.

Jim Deutch



Quote:
> OK, here's what I'm trying to do.

> I want to declare a static variable in the declarations section of a
class.
> This is capable of being done in C++ and Java but I haven't quite figured
> out how to do it in VB.

> What I need is a variable that is available to ALL objects created from
> this class, and when that variable changes, ALL objects see the new value
> of that variable.

> Any and all help is greatly appreciated,
> -Brian

> --
> --------------------------------------------
> Brian Orrell
> Dallas, TX

> fax: 214.522.1927



Tue, 05 Oct 1999 03:00:00 GMT  
 Static Variables in Classes

Brian, Private variables declared before the first procedure are static.
Therefore:
   dim m_myvar
And later on set property Let/Get routines for m_myvar:
   Public Property Let MyVar(vNew)
     m_myvar = vnew
   End Property

   Public Property Get Myvar()
      Myvar = m_MyVar
   End Property

Peter

Quote:

> OK, here's what I'm trying to do.

> I want to declare a static variable in the declarations section of a class.
> This is capable of being done in C++ and Java but I haven't quite figured
> out how to do it in VB.

> What I need is a variable that is available to ALL objects created from
> this class, and when that variable changes, ALL objects see the new value
> of that variable.

> Any and all help is greatly appreciated,
> -Brian

> --
> --------------------------------------------
> Brian Orrell
> Dallas, TX

> fax: 214.522.1927



Sat, 09 Oct 1999 03:00:00 GMT  
 Static Variables in Classes

STATIC variables... such as those in C++ and Java would work as follows

If my_static was the static variable...

Dim A As New ClsFoo
Dim B As New ClsFoo
Dim C As New ClsFoo

A.my_static = 3
Debug.Print B.my_static 'Would print 3
B.my_static = 12
Debug.Print A.my_static 'Would print 12
Debug.Print C.my_static 'Would print 12

The benefits of having static variables are many.  If you had a limit to
the number of objects that could be created from a certain class, you could
increment and decrement an object creation count as a static variable.
This would allow for checking how many objects were currently created from
the original class.  There are many other examples, but that's the first
that comes to mind.



Quote:
> Brian, Private variables declared before the first procedure are static.
> Therefore:
>    dim m_myvar
> And later on set property Let/Get routines for m_myvar:
>    Public Property Let MyVar(vNew)
>      m_myvar = vnew
>    End Property

>    Public Property Get Myvar()
>       Myvar = m_MyVar
>    End Property

> Peter


> > OK, here's what I'm trying to do.

> > I want to declare a static variable in the declarations section of a
class.
> > This is capable of being done in C++ and Java but I haven't quite
figured
> > out how to do it in VB.

> > What I need is a variable that is available to ALL objects created from
> > this class, and when that variable changes, ALL objects see the new
value
> > of that variable.

> > Any and all help is greatly appreciated,
> > -Brian

> > --
> > --------------------------------------------
> > Brian Orrell
> > Dallas, TX

> > fax: 214.522.1927



Sat, 09 Oct 1999 03:00:00 GMT  
 Static Variables in Classes

Brian,

Quote:
> STATIC variables... such as those in C++ and Java would work as follows

Yes, VB5 lacks static member variables, which I agree would be useful. In
fact, I've complained about this through several of the VB betas.
Hopefully, we'll see them in the future.

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com



Sat, 09 Oct 1999 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Static Class and Variable

2. Static variable not static enough ???

3. static classes and methods

4. creating static classes?

5. Static class data

6. Static class module in VBA

7. Static class attributes, how to create them.

8. Class Side (Static) functions

9. Static class membervariables?

10. C++ static class property possible in VB ?

11. Static class members

12. static class data in ActiveX DLLs?

 

 
Powered by phpBB® Forum Software