Click here to Skip to main content
15,886,664 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
cannot access a disposed object in c#
Posted
Updated 1-Jan-18 21:43pm
Comments
Sergey Alexandrovich Kryukov 25-Jan-11 22:47pm    
not a question
Sandeep Mewara 26-Jan-11 4:03am    
Is that a question?

In other words, you cannot use a discarded object. That's look fair to me: you should ask yourself why do you need an already disposed object, or why did you discard an object if you still need it.

:)
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 26-Jan-11 11:13am    
This is correct (my 5) but, as I explained in my answer, it should not prevent "accessing" object.
Disposing of object has nothing to do with its accessibility. Actually, objects are not even classified into disposed and non-disposed.

The method <code>Dispose itself has no predefined meaning, except using in the "using" construct, only if this method is written as implementation of IDisposable.Dispose.

The standard purpose of Dispose is 1) cleaning up unmanaged resources, 2) calling IDisposable.Dispose of other objects thus disposing a chain of related objects of some hierarchy of the object model (typical example: System.Windows.Forms.Form); the ultimate purpose of such chain of calls is -- again -- cleaning of associated unmanaged resources.

There can be some other uses of the interface and its implementation, mainly thanks to the "using" construct functionality. For a funny example, see my Tips/Tricks article (and alternative Tips on the same page; they are good): Hourglass Mouse Cursor Always Changes Back to its Original Image. How?[^].

This should explain the purpose of using and the fact it cannot affect accessibility of any object. Maybe, the question was caused to some different unrelated problem which was misinterpreted.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 26-Jan-11 11:15am    
@surucha18: if you still did not figure out what's wrong, you need to post your code -- we'll find out in not time. Not a valid reason to vote "3" (I did not say you voted, I don't know who.)
CPallini 26-Jan-11 14:34pm    
Have a 5. :-)
google with keywords

c# dispose

and you will find, that the reason for using Dispose is to clean up some stuff (as SAKryukov explained), and therefor you cannot access a disposed object.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 26-Jan-11 11:18am    
I don't think there is a reason not to access any object (except sloppiness :-) only because someone called Dispose. If the object is not functioning, this is a different issue -- then the problem is terminology -- "access".
It's meaning that you have closed any form and then the program cannot any access to form which is disposed. If there aren't information you can write that:
...{
[Your Form] f = new [Your Form];
if(f.IsDisposed){
f = new [Your Form];
}
f.Show();
}
else having information write it to any file and Write to it:


System.IO.StreamWriter sw = System.IO.File.CreateText(@"your path\\...\\your file.your file type");
if you don't want to write the information on new line
sw.Write();
else
sw.WriteLine();
 
Share this answer
 
Comments
Ralf Meier 2-Jan-18 4:15am    
Have you realized that the original question is nearly 7 years old ? Do you think that it is still an actuell problem ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900