How/where to declare variables shared by objects of a class?

J

joeu2004

How and where can I declare variables that are intended to be shared
by objects of a class?

For example, suppose I want each object of a class to have a unique
instance numbers. I have the following class module:

Private objInst As Long

Private Sub Class_Initialize()
objcnt = objcnt + 1
objInst = objcnt
End Sub

Property Get instNum() 'read-only
instNum = objInst
End Property

As written, objcnt must be a global or static variable somewhere that
is initially zero.

The only way I am able to do that is to declare Public objcnt in a
standard module. That violates the concepts of class encapsulation
and data hiding. It also exposes objcnt to corruption.
 
P

Peter T

joeu2004 said:
How and where can I declare variables that are intended to be shared
by objects of a class?

For example, suppose I want each object of a class to have a unique
instance numbers. I have the following class module:

Private objInst As Long

Private Sub Class_Initialize()
objcnt = objcnt + 1
objInst = objcnt
End Sub

Property Get instNum() 'read-only
instNum = objInst
End Property

As written, objcnt must be a global or static variable somewhere that
is initially zero.

The only way I am able to do that is to declare Public objcnt in a
standard module. That violates the concepts of class encapsulation
and data hiding. It also exposes objcnt to corruption.

As in your adjacent thread ObjPtr will give you a unique ID for the lifetime
of your object. So you could do say

Public myPtr As Long
Private Sub Class_Initialize()
myPtr = ObjPtr(Me)

Note once the object is destroyed the old pointer might be used for new
objects.

FWIW there is a way to assign another object variable to an object merely by
knowing its pointer.

Regards,
Peter T
 
J

joeu2004

joeu2004 said:
How and where can I declare variables that are
intended to be shared by objects of a class?
For example, suppose I want each object of a class
to have a unique instance numbers.
[....]
As in your adjacent thread ObjPtr will give you a
unique ID for the lifetime of your object. So you
could do say

Public myPtr As Long
Private Sub Class_Initialize()
myPtr = ObjPtr(Me)

Thanks for the "pointer" ;-). However, that response address my
example, not my question.

My question is: How and where can I declare variables that are
intended to be shared by objects of a class?

An instance number is simply the only use for this that I can think of
off the top of my head.

And suppose the instance number is intended to have a specific
structure, e.g. an alphabetic string. ObjPtr does not work in that
case, obviously.
 
P

Peter T

joeu2004 said:
My question is: How and where can I declare variables that are
intended to be shared by objects of a class?

Not sure I follow, but taking your question literally the answer is store at
module level in the normal way, either as a simple defined variable or
defined variable and Property pair.

Regards,
Peter T
 

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