Click here to Skip to main content
15,893,722 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone. I have this noob question, and I wouldn't like to bother anyone with it, but I couldn't find an answer on my own after like 3 hours of web-search.

So here I come. I have this class which I only use for storing different types of data ordered in properties (which also have some events fired so the user of the class can do something specific when something has changed, etc). Anyways, the class, as far as i know and understand, doesn't have any unmanaged resources. I wanted to implement a counter for the class instances so I tried with a
VB
Public Shared Property Count As Integer
and in the constructor i added
VB
TheClass.Count += 1

But the problem comes when i want to substract from that property, I mean, I can't know when an instance of my class has been disposed... Or do I? If so, How? Or else, Should I Implement IDisposable so I can have an explicit destructor and fire the event manually? (a.k.a. substract 1 at the disposing time)

Also, now that we are in subject, If I must unavoidably use IDisposable, can anyone share some links so I can learn like 'Step by step' everything related to managed, unmanaged types/objects, the garbage collector, and... I don't know what else could be of use?

Anyways, for the moment, if someone can only answer the first part of this question, I would be very grateful.

Thank you in advance.
Posted
Updated 19-Aug-14 19:36pm
v2

1 solution

Create a Destructor and decrement the value there inside the destructor.
 
Share this answer
 
Comments
Elighne 20-Aug-14 4:42am    
Yeah, that is what im asking, the 'create a destructor' thing. I must Use the IDisposable? or just adding the =Protected Overrides Sub Finalize() : MyBase.Finalize()=
and then just wait for the GC to -maybe- do his work so the shared var can update?
Mayank Vashishtha 20-Aug-14 4:49am    
It depends on you. Better is to use IDisposable then in Dispose() decrement the value. Also don't forget to suppress finalize method.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900