Keeping track of class instances 
Author Message
 Keeping track of class instances

I have an application with a class called cEdit. This class shows a
nonmodal form where the user can edit some data.

The application can load any number of instances of this class.

How can I keep track of all the instances and get rid of the instances
when the user closes the form?
I can handle the closing event of the form within the class but how do
I unload the current instance of the class in the closing event?

Here is the code of my class cEdit:

Public Class cEdit
    Public Title As String
    Dim WithEvents f As Form2

    Public Sub Show()
        f = New Form2()
        f.Text = Title
        f.Show()
    End Sub

    Private Sub f_Closing(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles f.Closing
        f = Nothing

    End Sub

    Public Sub New()
       'Keep track of number of instances in a global variable  
        ClassCount += 1
    End Sub

    Protected Overrides Sub Finalize()
        ClassCount -= 1
        MyBase.Finalize()
    End Sub
End Class

anywhere in my code I can create an instance for cEdit like this:

dim x as cEdit = new cEdit
x.Title = 'This is the title'
x.Show

Thomas



Wed, 21 Sep 2005 18:12:08 GMT  
 Keeping track of class instances


Quote:
> I have an application with a class called cEdit. This class shows a
> nonmodal form where the user can edit some data.

> The application can load any number of instances of this class.

> How can I keep track of all the instances and get rid of the instances
> when the user closes the form?
> I can handle the closing event of the form within the class but how do
> I unload the current instance of the class in the closing event?

> Here is the code of my class cEdit:

> Public Class cEdit
>     Public Title As String
>     Dim WithEvents f As Form2

>     Public Sub Show()
>         f = New Form2()
>         f.Text = Title
>         f.Show()
>     End Sub

>     Private Sub f_Closing(ByVal sender As Object, ByVal e As
> System.ComponentModel.CancelEventArgs) Handles f.Closing
>         f = Nothing

>     End Sub

>     Public Sub New()
>        'Keep track of number of instances in a global variable
>         ClassCount += 1
>     End Sub

>     Protected Overrides Sub Finalize()
>         ClassCount -= 1
>         MyBase.Finalize()
>     End Sub
> End Class

> anywhere in my code I can create an instance for cEdit like this:

> dim x as cEdit = new cEdit
> x.Title = 'This is the title'
> x.Show

> Thomas

In your example cEdit would actually be GC'd after it goes out of scope, but
to address your question:

The easiest way is to have cEdit _be_ the form.  Just change the form's name
from Form2 to cEdit.

Like this:

Public Class cEdit
  Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "
...
#End Region

  Public Property Title() As String
    Get
      Return Me.Text
    End Get
    Set(ByVal Value As String)
      Me.Text = Title

    End Set
  End Property

End Class

Then there's no need to have 2 different classes.

David



Wed, 21 Sep 2005 23:03:38 GMT  
 Keeping track of class instances
What if my cEdit class does not contain a form?

Thomas

On Sat, 5 Apr 2003 09:03:38 -0600, "David Browne" <davidbaxterbrowne

Quote:



>> I have an application with a class called cEdit. This class shows a
>> nonmodal form where the user can edit some data.

>> The application can load any number of instances of this class.

>> How can I keep track of all the instances and get rid of the instances
>> when the user closes the form?
>> I can handle the closing event of the form within the class but how do
>> I unload the current instance of the class in the closing event?

>> Here is the code of my class cEdit:

>> Public Class cEdit
>>     Public Title As String
>>     Dim WithEvents f As Form2

>>     Public Sub Show()
>>         f = New Form2()
>>         f.Text = Title
>>         f.Show()
>>     End Sub

>>     Private Sub f_Closing(ByVal sender As Object, ByVal e As
>> System.ComponentModel.CancelEventArgs) Handles f.Closing
>>         f = Nothing

>>     End Sub

>>     Public Sub New()
>>        'Keep track of number of instances in a global variable
>>         ClassCount += 1
>>     End Sub

>>     Protected Overrides Sub Finalize()
>>         ClassCount -= 1
>>         MyBase.Finalize()
>>     End Sub
>> End Class

>> anywhere in my code I can create an instance for cEdit like this:

>> dim x as cEdit = new cEdit
>> x.Title = 'This is the title'
>> x.Show

>> Thomas

>In your example cEdit would actually be GC'd after it goes out of scope, but
>to address your question:

>The easiest way is to have cEdit _be_ the form.  Just change the form's name
>from Form2 to cEdit.

>Like this:

>Public Class cEdit
>  Inherits System.Windows.Forms.Form

>#Region " Windows Form Designer generated code "
>...
>#End Region

>  Public Property Title() As String
>    Get
>      Return Me.Text
>    End Get
>    Set(ByVal Value As String)
>      Me.Text = Title

>    End Set
>  End Property

>End Class

>Then there's no need to have 2 different classes.

>David



Wed, 21 Sep 2005 23:34:06 GMT  
 Keeping track of class instances


Quote:
> What if my cEdit class does not contain a form?

> Thomas

Big picture: You are not responsible for cleaning up your own objects.  The
Garbage Collector will do this for you.

Small picture: On closer review of your example, cEdit has an event handler
for the Form2 event.  This means not only does each instance of cEdit hold a
reference to the instance of Form2, but each instance of Form2 has a
reference to the instance of cEdit that created it.  (The event handler is
implemented by giving Form2 a delegate which points to a method on cEdit,
and the cEdit instance).

In this situation Form2 will keep the instance of cEdit alive.  When an
instance of Form2 becomes elegible for garbage collection, so will its
related cEdit.

David



Thu, 22 Sep 2005 01:14:29 GMT  
 Keeping track of class instances
David,
thanks for your help. I have changed my code somewhat.
Whenever I closed one of the forms the class would still be there. To
check this I have added some code in the Finalize event.
When I also added a GC.Collect the class was destroyed and everything
works fine.

Thomas

On Sat, 5 Apr 2003 11:14:29 -0600, "David Browne" <davidbaxterbrowne

Quote:



>> What if my cEdit class does not contain a form?

>> Thomas

>Big picture: You are not responsible for cleaning up your own objects.  The
>Garbage Collector will do this for you.

>Small picture: On closer review of your example, cEdit has an event handler
>for the Form2 event.  This means not only does each instance of cEdit hold a
>reference to the instance of Form2, but each instance of Form2 has a
>reference to the instance of cEdit that created it.  (The event handler is
>implemented by giving Form2 a delegate which points to a method on cEdit,
>and the cEdit instance).

>In this situation Form2 will keep the instance of cEdit alive.  When an
>instance of Form2 becomes elegible for garbage collection, so will its
>related cEdit.

>David



Thu, 22 Sep 2005 14:59:15 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Tracking class instances

2. newbie: private class variables are visible in class instances

3. Find out if somebody make an instance of a Class without having access to the Class

4. nested class inherits containing class accessing private instance members

5. instance a class from its class name

6. keeping Track Of Record

7. Keeping Track of Database Modifications?!?!

8. Keeping track of Users Online

9. Keeping track of Database changes!?

10. KEEPING TRACK OF WORD DOC FILES

11. Keeping track of all those functions!?!?!

12. Keeping track of the time that Outlook was shutdown

 

 
Powered by phpBB® Forum Software