Results 1 to 7 of 7

Thread: Disposed Object Issue

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2010
    Posts
    36

    Disposed Object Issue

    Currently I have two separate solutions. Both are windows form applications. Both have a button on Form1 to open Form2. The code to open Form2 is Form2.Show(). On my first app. I can close Form2 using either the red "X" or using a button that does Me.Hide() I can then reopen Form2 by clicking the button on Form1 that does Form2.Show()


    On my second app. however, I have the same setup, but if I close Form2 using the red "X" and try reopening it from Form1 using the button that calls Form2.Show(), I get an error about cannot access Disposed object.


    I'm not sure what, If anything I have changed or why this could be happening. Any thoughts on were I can begin my hunt for answers would be great! Thanks!

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Disposed Object Issue

    It sounds like the first project is using the default instance while the second is not. Are you using the New key word with Form2 in either or both projects?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Member
    Join Date
    Jan 2010
    Posts
    36

    Re: Disposed Object Issue

    Hmm... not sure.

    Is there a simple work around to this? Like creating a new instance of a form after it has been disposed of?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Disposed Object Issue

    The default instance should take care of that. If an instance exists that hasn't been disposed then that instance should be used, otherwise a new instance should be created and that used. All that logic should be internal. The whole point of default instances is that the system manages that sort of thing and you don't have to worry about it at all. That's why I think that you must NOT be using the default instance in the project that has the issue.

    One solution is to simply not use the default instance at all. You'll find that that's what most experienced developers do, myself included. If you want a form then you create a form, just like any other type. If you need to refer to a form more than once then you assign it to variable. You can then get the form from that variable and test its IsDisposed property to decide whether you need to create a new instance. You can follow the CodeBank link in my signature and check out my Singleton Form thread for an example. You might use that class of mine or you may not, but the logic of creating, assigning and testing the form instance would be the same.

    I'd still be interested to see the actual code you're using though, as it sounds strange that two identical projects would behave differently.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Member
    Join Date
    Jan 2010
    Posts
    36

    Re: Disposed Object Issue

    Ah, they aren't "identical" I was kind of paraphrasing as an example...

    Anyways, what you say makes sense. I will check into your code and see if I can apply it in my project. Thanks!

  6. #6

    Thread Starter
    Member
    Join Date
    Jan 2010
    Posts
    36

    Re: Disposed Object Issue

    I found some work around code for this problem... I know it would be best practice to resolve the issue all together. But for now I am able to make it work:

    vb.net Code:
    1. Private Sub userEditor_exit(ByVal sender As Object, ByVal e As FormClosingEventArgs) Handles Me.FormClosing
    2.         e.Cancel = True
    3.         Hide()
    4.     End Sub

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Disposed Object Issue

    Quote Originally Posted by Suterusu View Post
    I found some work around code for this problem... I know it would be best practice to resolve the issue all together. But for now I am able to make it work:

    vb.net Code:
    1. Private Sub userEditor_exit(ByVal sender As Object, ByVal e As FormClosingEventArgs) Handles Me.FormClosing
    2.         e.Cancel = True
    3.         Hide()
    4.     End Sub
    That looks like a complete hack and should never be required if the rest of the code is written properly. Writing some bad code to get around a problem created by some other bad code is a slippery slope. Also, that code looks like it will prevent Windows shutting down because that form will never close.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width