Cannot access a disposed object in instance VB.net

i have problem with my project, i have 2 form, 1 form with MDI container = true and 1 form is child form. i have code in parent form like below

   Public Class frmInduk
    Dim afrmDaftarBarang As frmDaftarBarang = frmDaftarBarang.instance
    Private Sub DataBarangToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataBarangToolStripMenuItem.Click

        With afrmDaftarBarang
            .MdiParent = Me
            'Dim afrmDaftarBarang As frmDaftarBarang = frmDaftarBarang.instance
            .Show()
            .Focus()

        End With
    End Sub
End Class

and than, in my child form i have code like below

Public Class frmDaftarBarang
Private Shared anInstance As frmDaftarBarang
Public Shared ReadOnly Property instance() As frmDaftarBarang
    Get
        If anInstance Is Nothing Then
            anInstance = New frmDaftarBarang

        End If
        Return anInstance

    End Get
End Property

End Class

when i run this project, it no problem . my project can run very well. but the truble come when i call frmDataBarang, and then i close, but when i call frmDataBarang again i get some error lice picture bellow

i hope anybody can help me :(

Answers


You are trying to use the Singleton pattern, but you did it imperfectly. An MDI child form can be created more than once, you will have to set the singleton instance back to Nothing when it gets destroyed. Do so with the FormClosing event:

Protected Overrides Sub OnFormClosed(ByVal e As FormClosedEventArgs)
    anInstance = Nothing
    MyBase.OnFormClosed(e)
End Sub

You will also need to do something reasonable when an existing instance is displayed again. It might not be in the right state. This code really belongs in the MDI parent, but you can limp along with this:

    Get
        If anInstance Is Nothing Then
            anInstance = New frmDaftarBarang
        Else
            If anInstance.WindowState = FormWindowState.Minimized Then
                anInstance.WindowState = FormWindowState.Normal
            End If
        End If
        Return anInstance

Do note that this gets ugly when you use the singleton to do things other than child activation. It is really rather a bad idea to use the pattern. It is fine on your MdiParent, there can only ever be one parent and when it gets closed then the program terminates anyway. But those same rules don't apply to a child. Move child instance management code to the parent class, that's where it belongs.


Need Your Help

SQL query to calculate time spans based upon time stamps

c# sql sqlite

In my project, I have need of calculating a time span. Currently, I'm retrieving every TimeStamp that matches my query and storing them in a List<>. Then, I iterate through the list to see if...

Is there a working package for native FB login?

meteor meteor-accounts

The most-recently-updated one I could find was https://github.com/particle4dev/meteor-accounts-facebook-cordova (repo way ahead of atmosphere version), but I wasn't able to get it working (see last...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.