Proper Declare/Dispose of Objects in VB.NET

Posted on 2012-08-28
Last Modified: 2012-08-29

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.

Question by:Peter Allen
    LVL 85

    Accepted Solution

    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

    LVL 40

    Expert Comment

    by:Jacques Bourgeois (James Burger)
    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.

    Author Closing Comment

    by:Peter Allen

    Seeing the code snipit you are absolutely correct.  Thank you!  You ROCK!
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Article by: Kraeven
    Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
    A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    This video discusses moving either the default database or any database to a new volume.

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now