Solved

dispose of a DataTable created in another class

Posted on 2016-10-23
4
22 Views
Last Modified: 2016-10-25
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
Comment
Question by:dhenderson12
4 Comments
 
LVL 49

Assisted Solution

by:Ryan Chong
Ryan Chong earned 125 total points
ID: 41856547
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
 
LVL 62

Assisted Solution

by:Fernando Soto
Fernando Soto earned 125 total points
ID: 41857017
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
 
LVL 19

Accepted Solution

by:
Daniel Van Der Werken earned 250 total points
ID: 41857747
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
 

Author Closing Comment

by:dhenderson12
ID: 41858637
Thanks to all for your help.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now