|
-
Jan 30th, 2010, 02:31 PM
#1
Thread Starter
Member
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!
-
Jan 30th, 2010, 08:59 PM
#2
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?
-
Jan 30th, 2010, 09:09 PM
#3
Thread Starter
Member
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?
-
Jan 31st, 2010, 01:47 AM
#4
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.
-
Jan 31st, 2010, 02:04 AM
#5
Thread Starter
Member
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!
-
Jan 31st, 2010, 02:42 PM
#6
Thread Starter
Member
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:
Private Sub userEditor_exit(ByVal sender As Object, ByVal e As FormClosingEventArgs) Handles Me.FormClosing
e.Cancel = True
Hide()
End Sub
-
Jan 31st, 2010, 07:16 PM
#7
Re: Disposed Object Issue
 Originally Posted by Suterusu
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:
Private Sub userEditor_exit(ByVal sender As Object, ByVal e As FormClosingEventArgs) Handles Me.FormClosing
e.Cancel = True
Hide()
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|