469,331 Members | 5,487 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,331 developers. It's quick & easy.

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 7681
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Willem van Rumpt | last post: by
4 posts views Thread by phl | last post: by
6 posts views Thread by Water Cooler v2 | last post: by
11 posts views Thread by Mark Rae | last post: by
47 posts views Thread by Hilton | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.