PDA

View Full Version : How to get the number of instances of a class



Le Pat
05-29-2021, 08:18 AM
Hi everybody !
I'm trying to count the number of instantiations of a class.


Typically, in the context of 'Object', it speaks here of a class variable (and not an instance variable). This variable will increment by 1 each time the class is instantiated.


Example :
There is a class S1 in a class module.


On a form:


dim elem1 as S1
dim elem2 as S1
set elem1 = new S1
set elem2 = new S1

Here, I'd like to obtain the result is 2 and to get this value from the variable in the class S1.

Do you have a small example on hand?


Thank you in advance for your answers !

snb
05-29-2021, 08:44 AM
With what purpose do you want to know this ?

Le Pat
05-29-2021, 09:30 AM
With what purpose do you want to know this ?

Well, it's for a more complicated project ;o)))
I just want to know if it's doable and if so, how.

snb
05-29-2021, 12:10 PM
If we answer your question the way you answer mine, you shouldn't be optimistic.

SamT
05-29-2021, 01:00 PM
The only way I can imagine is to increment an external counter every time the class is initialized

Private Sub Class_Initialize()
ExternalVariable = ExternalVariable + 1
End Sub

Private Sub Class_Terminate()
ExternalVariable = ExternalVariable - 1
End Sub



ExternalVariable must be Public

Le Pat
05-29-2021, 02:18 PM
The only way I can imagine is to increment an external counter every time the class is initialized

Private Sub Class_Initialize()
ExternalVariable = ExternalVariable + 1
End Sub

Private Sub Class_Terminate()
ExternalVariable = ExternalVariable - 1
End Sub



ExternalVariable must be Public

Yes, it's the best thing we can do, you're right...

Many thanks for your help !