Solved

dispose of a DataTable created in another class

Posted on 2016-10-23
4
64 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 52

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 63

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 20

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

739 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