Class instance count.

  • Thread starter Thread starter Per Forsgren
  • Start date Start date
P

Per Forsgren

I have a DLL with a public class (MyClass).
The class is instantiated into several instances in a Windows form with :

Protected WithEvents MyInst01 As New MyClass.MyClass
Protected WithEvents MyInst02 As New MyClass.MyClass
Protected WithEvents MyInst03 As New MyClass.MyClass
Protected WithEvents MyInst04 As New MyClass.MyClass

The class has a public "Init" Function that is called from the Window form
in order to activate the functionality for each instance.

Now to the question:
Is there any way to count the number of activated instances from inside the
"Init" Function in the class. The reason is that I want to limit the use of
the class and reject over-use of it. I am open to other approaches, but they
need to be self contained from within the "Init" Function in the class.

Possible or not?

//Per
 
* "Per Forsgren said:
I have a DLL with a public class (MyClass).
The class is instantiated into several instances in a Windows form with :

Protected WithEvents MyInst01 As New MyClass.MyClass
Protected WithEvents MyInst02 As New MyClass.MyClass
Protected WithEvents MyInst03 As New MyClass.MyClass
Protected WithEvents MyInst04 As New MyClass.MyClass

The class has a public "Init" Function that is called from the Window form
in order to activate the functionality for each instance.

Now to the question:
Is there any way to count the number of activated instances from inside the
"Init" Function in the class. The reason is that I want to limit the use of
the class and reject over-use of it. I am open to other approaches, but they
need to be self contained from within the "Init" Function in the class.

You can set up a shared variable inside the class and increment this
variable in the 'Init' procedure:

\\\
Private Shared m_InstanceCount As Integer

Public Sub Init()
m_InstanceCount += 1
...
End Sub
///
 
Per,
In addition to Herfried's suggestion of a shared variable.

I would seriously consider naming your "Init" function New, as this will
cause it to be the constructor, which will require it to be called!

Private Shared m_InstanceCount As Integer

Public Sub New()
m_InstanceCount += 1
If m_InstanceCount = 10 Then
Throw New OverUseException()
End If
...
End Sub

This will prevent any more then 10 instances of your class from being
created. Of course you may want to implement IDisposable, to allow users of
your class to let you know that they are done with your class. In which case
I would case IDisposable.Dispose to set a flag to know that this instance is
invalid & throw an exception if any of the methods are called.
IDisposable.Dispose would also decrement m_InstanceCount.

Note you would need to define the OverUseException.

Hope this helps
Jay
 

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

Back
Top