473,325 Members | 2,480 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

IDisposable with an abstract class?

I have a database interface class (an abstract class) that has two
derived classes that work with two different types of databases. If
the dervied classes have managed, and unmanged resources and the base
(abstract) class doesn't, how do I implement the IDisposable interface?
Do I have the base (abstract) class inherit from IDisposable? If I
do, how will that work since the class is abstract and no instance is
actually created. Or, should I just implement the interface
IDisposable off of the two derived classes?

Thanks!

bubby

Nov 16 '05 #1
3 8309
bubby,

In this case, you would implement IDisposable on the abstract class. On
your abstract class, you would have a method with the following signature:

protected virtual void Dispose(bool disposing)
{
}

This is what would be overridden in base classes to dispose their
resources. When implementing this, the boolean disposing value is going to
tell you whether or not you are being called from the finalizer or from the
Dispose method.

Check out the article on MSDN titled "Implementing Finalize and Dispose
to Clean Up Unmanaged Resources", located at (watch for line wrap):

http://msdn.microsoft.com/library/de...izeDispose.asp

It goes into great detail how you should implement IDispose on your base
class, as well as override the protected Dispose method in your derived
classes.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"bubby" <aj******@yahoo.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
I have a database interface class (an abstract class) that has two
derived classes that work with two different types of databases. If
the dervied classes have managed, and unmanged resources and the base
(abstract) class doesn't, how do I implement the IDisposable interface?
Do I have the base (abstract) class inherit from IDisposable? If I
do, how will that work since the class is abstract and no instance is
actually created. Or, should I just implement the interface
IDisposable off of the two derived classes?

Thanks!

bubby

Nov 16 '05 #2
Probably this is a good way:

In the abstract class (called MyAbstractClass):

bool disposed;
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if(!this.disposed)
{
if(disposing)
{
// Dispose managed resources.
}
// Dispose unmanaged resources here.
}
disposed = true;
}
~MyAbstractClass()
{
Dispose(false);
}
In the 2 derived classes:

protected override void Dispose(bool disposing)
{
if(disposing)
{
// Dispose managed resources.
}
// Dispose unmanaged resources here.
base.Dispose(disposing);
}

I hope this helped.
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Nov 16 '05 #3
Will this method work with an abstract class? This differs from a
regular class where because no instance is actually created. If that
is the case, how will the dispose method and/or the destructor ever get
called (ie. nothing was created)?

Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Willem van Rumpt | last post by:
Hi all, coming from an unmanaged programming background, I took my time to sort out the IDisposable and finalizer patterns. Just when I thought I had it all conceptually neatly arranged, the...
3
by: Dave | last post by:
I trying to determine the best pattern for designing my business and data layers... Can the instance of the business object eventually cause memory leaks in Example 1? If your business class...
4
by: Helge Jensen | last post by:
In C# 2.0 System.IO.Stream is declared as: public class Stream: ..., IDisposable { ... public void Dispose(); public void Dispose(bool); IDisposable.Dispose(); } Which must be a...
4
by: phl | last post by:
hi, My question is: 1. To avoid possible memory leaks, when you use this pattern, after you have dealth with the unmanaged resources and before you take your object off the finalize queue,...
6
by: Water Cooler v2 | last post by:
I heard from someone that we must not implement IDisposable for all classes. Can someone please tell me: 1. the reason why we must not implement IDisposable for all the classes we write. 2....
11
by: Mark Rae | last post by:
Hi, Following on from the recent thread about why HttpWebRequest doesn't implement the IDisposable interface, I got to wondering whether people make their custom classes inherit IDisposable or...
47
by: Hilton | last post by:
Hi, I'm sure I'm simplifying things here, but how about if the GC did this to objects that implement IDisposable: 1. Always Generation 1 (I think that is the correct name) 2. Get aggressive...
4
by: cgarcia0117 | last post by:
For any class I write in C# that has a member variable that implements IDisposable my class implements the IDisposable pattern. I do this to guarantee the reference to the member is explicitly...
34
by: Creativ | last post by:
Why does Thread class not support IDisposable? It's creating quite some problem. Namely, it can exhaust the resource and you have not control over it.
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.