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
"Per Forsgren" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
>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
>
>
>
>
|