Click here to Skip to main content
14,880,254 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
It is my understanding that I can use multiple interfaces with my class but i'm very confused with some of the articles about this. So here's my issue:-

I have an abstract class called driver
C#
public abstract class Driver

which I use to base my "Drivers" on
C#
class Driver_04 : Driver

Let's now imagine that I also want to have a "Dispose" method; in my mind, I should be able to do this
C#
class Driver_04 : Driver,IDisposable
{
...
   public override void Dispose()
   {
   }

}

however the compiler wont alow this but also, articles' suggest that the IDisposable should be added to my abstract class and the method Dispose included too. To me this makes no sense because the interface IDisposable is redundant in this instance ?
Can someone please explain ? Also, assuming it is obvious what I am trying to achieve, how would you do it ?
Posted
Comments
charles henington 12-Apr-13 5:40am
   
By adding IDisposable to the driver class this makes all classes that derive from the driver class disposable. Now you wont want to do the same thing for every class that derives from driver as each class will be independant on the dispose call? That being said in the driver class make the public void Dispose(){//code here} to public abstract void Dispose();. this forcesses you to implement the Dispose Method in each class as an override and allows you to use as needed for that class hope this helps

C#
class Driver_04 : Driver,IDisposable
{
...
   public override void Dispose()
   {
   }
}


You cannot override the Dispose() method as it does not exist in the base class.
   
I'm a little confused:
"however the compiler wont alow this"
Yes it will:
C#
public abstract class Driver
    {
    public abstract void DoIt();
    }
public class DDriver : Driver, IDisposable
    {
    public override void DoIt()
        {
        throw new NotImplementedException();
        }

    public void Dispose()
        {
        throw new NotImplementedException();
        }
    }
Compiles cleanly with no errors or warnings on my system (VS2010) as I would expect.

However, I would do it this way:
C#
public abstract class Driver : IDisposable
    {
    public abstract void DoIt();
    public abstract void Dispose();
    }
public class DDriver : Driver
    {
    public override void DoIt()
        {
        throw new NotImplementedException();
        }

    public override void Dispose()
        {
        throw new NotImplementedException();
        }
    }
Because then you are forcing each derived class to implement IDisposable, which means that the abstract class variables can be used in using blocks irrespective of what eventual class they contain. If you implement IDisposable on one Derived class, then you have to check and convert each abstract class instance to see if it requires a Dispose call.
   
C#
public abstract class Driver : IDisposable
{
    public abstract void Dispose();
}

public class Driver_04 : Driver
{
    public override void Dispose()
    {
        // use dispose call as needed for this class
    }
}
public class Driver_03 : Driver
{
    public override void Dispose()
    {
        // use dispose call as needed for this class
    }
}
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900