Instances of an object

  • Thread starter Thread starter J L
  • Start date Start date
J

J L

Is there a way in VB.Net to determine the number of instances of an
object? And thier name?

e.g. John as Person, Linda as Person, Tom as Person
Find out there are 3 instances and thier names...I may want a routine
to dispose of any open instances.

TIA
John
 
You can add a Shared variable (static in C#) to keep track and
increment the variable in the constructor (Sub New):


Private Shared m_InstanceCount As Integer

Shared ReadOnly Property InstanceCount As Integer
Get
Return m_InstanceCount
End Get
End Property

Public Sub New()
m_InstanceCount += 1
End Sub
 
Chris,

Chris Dunaway said:
You can add a Shared variable (static in C#) to keep track and
increment the variable in the constructor (Sub New):

Private Shared m_InstanceCount As Integer

Shared ReadOnly Property InstanceCount As Integer
Get
Return m_InstanceCount
End Get
End Property

Public Sub New()
m_InstanceCount += 1
End Sub

In addition to that, you may want to decrement the counter in the class'
finalizer.
 
Back
Top