PC Review


Reply
Thread Tools Rate Thread

Class instance count.

 
 
Per Forsgren
Guest
Posts: n/a
 
      7th Sep 2004
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




 
Reply With Quote
 
 
 
 
Herfried K. Wagner [MVP]
Guest
Posts: n/a
 
      7th Sep 2004
* "Per Forsgren" <(E-Mail Removed)> scripsit:
> 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
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
 
Reply With Quote
 
 
 
 
Per Forsgren
Guest
Posts: n/a
 
      7th Sep 2004
Sometimes the the obvious is too difficult.
Thanks.
//Per




"Herfried K. Wagner [MVP]" <hirf-spam-me-(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>* "Per Forsgren" <(E-Mail Removed)> scripsit:
>> 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
> ///
>
> --
> M S Herfried K. Wagner
> M V P <URL:http://dotnet.mvps.org/>
> V B <URL:http://dotnet.mvps.org/dotnet/faqs/>



 
Reply With Quote
 
Jay B. Harlow [MVP - Outlook]
Guest
Posts: n/a
 
      8th Sep 2004
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
>
>
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can I make a Common Array Class and create a instance of the Common Array Class with Type of a new class Peri Microsoft C# .NET 10 17th Oct 2007 07:37 AM
Can I make a Common Array Class and create a instance of the Common Array Class with Type of a new class Peri Microsoft VB .NET 10 17th Oct 2007 07:37 AM
Creating a subclass instance from a base class instance Larry Lard Microsoft VB .NET 2 28th Jan 2005 06:48 PM
Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class. DJ Dev Microsoft ASP .NET 3 8th Feb 2004 05:19 PM
RE: VB .NET BUG: IntelliSense shows protected members of class instance in derived class Benjamin Wulfe Microsoft Dot NET Compact Framework 0 28th Jul 2003 11:29 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:02 PM.