• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 96
  • Last Modified:

dispose of a DataTable created in another class

I have a windows form and a class.  One of the methods in the class creates a DataTable and returns it to the calling form method.  Do I need to explicity dispose of the DataTable when I'm done with it?  If so, how?
0
dhenderson12
Asked:
dhenderson12
3 Solutions
 
Ryan ChongCommented:
I just read this and it could be help explained to you.

Should Dispose be called on DataTable and DataSet objects?
http://web.archive.org/web/20081203232123/http://www.devnewsgroups.net/group/microsoft.public.dotnet.framework.adonet/topic42917.aspx

...the system.data namespace (ADONET) does not contain
unmanaged resources. Therefore there is no need to dispose any of those as
long as you have not added yourself something special to it.
0
 
Fernando SotoCommented:
Hi  dhenderson12;

You do not need to call DataTable.Dispose() because the object does not contain any unmanaged components and therefor no need execute Dispose method. Now if you were to somehow attach a unmanaged object to the DataTable then you would need to call dispose on it. You can read this web page  Using Objects That Implement IDisposable to see examples.
0
 
Daniel Van Der WerkenIndependent ConsultantCommented:
Seems the overall recommendation is that you don't need to call .Dispose() even though the DataTable class does implement a class that implements IDisposable.

A little research indicates that the class that DataTable implements that implements IDisposable is MarshalByValueComponent.

You can read the definition, but it appears to be that if you're implementing remotable components that are marshalled by value -- passing copies of serialized objects -- then you need to Dispose.

I don't think anyone does this. So, you're likely extremely safe not calling Dispose or worrying about it. I've used DataTables for years without Dispose and no one has yelled at me, yet.

If you really, really, really want to Dispose, then do it in a using statement like this:

using (DataTable dt = myClassAsInstanceObject.CreateCoolDataTable())
{
   // do some cool data stuff
}

Open in new window

0
 
dhenderson12Author Commented:
Thanks to all for your help.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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