Proper Declare/Dispose of Objects in VB.NET


I am working on an application that uses classes to open one to two forms simultaniously.  The first execution of the aplication works fine and I can close the open forms, but upon executing a different menu option I am getting an "ObjectDisposedException" error.

I believe the problem is how I am closing/handling the Open/Close process so I wanted to ask for your assistance.  I haven't shown any code, but want to use an analogy because I thing it would be shorter and better illustrate what I want to have happen.

I have two menus, one called Project and the other Designs.  Both menus have "New" and "Open".

New will open two forms, Editor and Catalog.  The user works with the entry and can view select entries with the Catalog.  Once the user is inished he/she clicks close on the Editor and both the Editor and Catalog close.  The Catalog cannot close until the Editor is closed.

Open differs from New in that the Catalog is opened first.  In this case the user selects an entry which is saved to a variable.  The Catalog form is closed and the Editor/Catalog are opened and the selected entry is displayed as with New above accept that it is an existing entry.

The closure of the Catalog with Open gives me the error I mentioned.

Given the analogy aboe can you give me an example of the code I would need to add when I close the Editor form.  If you need to see portions of the actual code just let me know.

BTW as stated above I do want the Catalog to close in the Open menu option above and be re-opened with Editor rather then to leave it open if I can do that.

Peter AllenAsked:
Who is Participating?
Mike TomlinsonMiddle School Assistant TeacherCommented:
This is a common error.  Based on your PAQ here: just need to create a new instance of the form if it is currently "Nothing" or was previously disposed of:
Public Class clsExample

    Public Shared f_Form As New frmEditor
    Public Shared f_Catlg As New frmCatalog

    Public Shared Sub subOpenProject()
        If IsNothing(f_Form) OrElse f_Form.IsDisposed Then
            f_Form = New frmEditor
        End If

        If IsNothing(f_Catlg) OrElse f_Catlg.IsDisposed Then
            f_Catlg = New frmCatalog
        End If
    End Sub

End Class

Open in new window

Jacques Bourgeois (James Burger)PresidentCommented:
Do you really need to close the forms. If you know they will reopen, you could simply hide them and refresh the display in the VisibleChanged event. In some situations, this even enhance performance because the data is already loaded when the user calls the form a second time.
Peter AllenAuthor Commented:

Seeing the code snipit you are absolutely correct.  Thank you!  You ROCK!
Mike TomlinsonMiddle School Assistant TeacherCommented:
James brings up a valid point, too.  You can keep the form open by setting "e.Cancel = True" in the FormClosing() event, and then setting Visible() to False.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.