Solved

dispose of a DataTable created in another class

Posted on 2016-10-23
4
71 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
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…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

691 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