How to find that a Class has a instance ?

R_K_T_ASHOKA

Active member
Joined
Apr 6, 2006
Messages
33
Programming Experience
3-5
HI

How to find that a Class has been instantiated..? Is there any way ??

Thanks

bye
 
In your class you declare a variable to store the number of instances running :
VB.NET:
Private Shared _Instances As Integer = 0

You add a constructor to your class :
VB.NET:
Public Sub New()
        InitializeComponent()
        _Instances += 1
        Me.Text = "Instance # " & _Instances.ToString
        ' Add any initialization after the InitializeComponent() call.
    End Sub
To access the result you need a shared property that is accessible even if no class has been instanciated:
VB.NET:
 Public Shared ReadOnly Property Instances()
        Get
            Return _Instances
        End Get
    End Property
And when you close a form you must decrease the number of instances :
VB.NET:
  Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        _Instances -= 1
        If disposing AndAlso components IsNot Nothing Then
            components.Dispose()
        End If
        MyBase.Dispose(disposing)
    End Sub

This code might be a little diffrent in VB 2003.
 
*blink* I thought the post was asking how to check if a class has been instantiated, rather than to ask how to monitor the number of current instances?

*scratch* in terms of determining whether a class has been instantiated, you need to know whether or not it has any memory allocated and such... the way I generally do this is to check if it is Nothing...
if IsNothing(Class) then "the class is not instantiated"

I dunno if I'm even on the same page here but oh well...
 
Yes, i need to know how we can find out whether a class has instances using any builtin method or something.. and i know the shared members way.. Thanks Bob Langlade

plz tell me if you know any builtin method to find that whether a class is instantiated that is how to find that a class has instances..
 
Sorry UncleRoning I am more used to checking if a form has been instantiated that is why I have suggested this code, because when you close a form (with the close box) the IsNothing(Form reference) will still return False.

If R_K_T_ASHOKA doesn't want to check Forms than your solution is a lot faster to implement, but mine will work with all classes.
 
there's probably some reflective route to take a look at the objects on the heap, you could then count the number of the type you are expecting, but warning.. there may be thousands of objects on the heap. i dont expect that enumerating them will be an easy, quick or useful operation...


In this case, we actually use design patterns.. the shared member way is evidence of this way. I would actually make a class factory for whichever class youre making, then have the factory count the number of objects it has created, and or store references to them. Its personal choice where you choose to encapsulate this and conceptually, there's no difference between the 2 methods.. just different ways of reaching the goal
 
*scratch* okay... I dont really get why isNothing() wouldnt work with forms? But how about this for checking if something is instantiated: Try...Catch
Try and manipulate the object. If it throws a Null exception then it isnt instantiated. So catch the exception should it occur and you have your answer. Its a pretty simple solution I guess. I dont know how fast it will be though
 
Thanks everybody..

Hi Paszt, i was wondering whether there is a builtin functionality that we can use to find that a class is instantiated or not..

for more clarity...

We have a class as follows..

VB.NET:
Class Sample
  Dim i as Integer
 
  Sub Disp()
   MsgBox(i)
  End Sub
..
..
End Class

and i am instantiating an object for the above class..

VB.NET:
Dim Obj as New Sample

Now can we find that class Sample has an object named Obj....

Thanks
 
@bob: will take your word for it, dont feel like opening VS (*whispers* I have a slow pc)... would that maybe have something to do with GC?

@RKT: I dont quite get want you want? You cant determine if Sample has a specific object unless that object is accessible globally, so you have to declare it as such.
 
UncleRonin said:
@bob: will take your word for it, dont feel like opening VS (*whispers* I have a slow pc)... would that maybe have something to do with GC?

To check that, I have created 200 instances of a Form in an array, then I have closed them using the close method. The garbage collector seems to get rid of them without using the Finalize methods as for other object, therefore IsNothing always return "No".
 
just because a form is closed, doesnt mean it's nothing!

try setting them to Nothing, then Is Nothing will return true. :)

Is Nothing merely determines whether your variable points to nothing. calling myForm.Close cannot set myForm = Nothing, by design..

All those 200 forms.. go call Show on them all again and they will all reapper, proving they are still hanging round in memory :)
 
R_K_T_ASHOKA,


I think youre getting confused between classes, objects and instances

A class is a blueprint object for an instance object. Everything is an object here, so we should stop referring to them as objects.

When an instance of a class is created it goes in memory somewhere. Then, we say that the type of the instance is (name of class)

In your case, you have an instance of Sample, referred to by a variable named obj. Sample, the class, does not own this instance and Sample (as a class) does not have instances. The heap (computer memory) might hold objects that are instances of the class type Sample, but not the other way round.

Like i said before, you either keep a count yourself, in the constructor (Factory pattern - each object must have a link to the factory that created it and when it is being disposed of it informs the factory thus) or you enumerate the heap and find out how many instaces of type X the heap holds. I dont know how or if this is possible, but it would be a reflective method and not recommended


Here's a better idea: You should tell us why you want to do this. Frequently people come here and they say "I have a problem and the solution i have devised is X, but i cant make it work. What is wrong with my solution?"
What they should be asking is "Here is my goal. How can i reach my goal?"

The difference is that we solve your actual problem, not the difficulty youre encountering with your particular solution to your problem (which may not be the best solution). SO.. tell us the actual problem?
 
Last edited by a moderator:
Sorry for delayed reply..

Hi all

cjard..

sorry for delayed reply...

I already said i was wondering whether there is a way to do this.. and so i dont have any goal..

Now I think there is no way to find using a class's name that a object exists for it..

If anyone thinks we have a way tell me..I need to know whether there is a builtin functionality, no major workarounds to do this..


Thanks
 
Back
Top