Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 272
  • Last Modified:

Proper Declare/Dispose of Objects in VB.NET

Experts,

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.

Thanks!
0
Peter Allen
Asked:
Peter Allen
  • 2
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
This is a common error.  Based on your PAQ here:
http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_27779110.html#a38156599

...you 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
        f_Form.Show()

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

End Class

Open in new window

0
 
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.
0
 
Peter AllenAuthor Commented:
Idle_Mind,

Seeing the code snipit you are absolutely correct.  Thank you!  You ROCK!
0
 
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.
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now