0

I want to create and abstract class A that implements IComparer so that I can implement different forms of comparing in the subclasses and later on do:

A sortBy = new B();
A sortBy2 = new C();

I'm new in C# and I don't quite get how to do that. I want them to implement this variations of this method:

int IComparer<Flower>.Compare(Flower a, Flower b)
        {
            if (a.leaves > b.leaves)
                return 1;
            if (a.leaves< b.leaves)
                return -1;
            else
                return 0;
        }

But I can't use abstract or override on it because it won't compile. Could you give me an idea of how to implement this? Thanks

moondaisy
  • 4,303
  • 6
  • 41
  • 70

2 Answers2

1

You could try something like this:

abstract class Flower : IComparer<Flower>
{
    public int Leaves { get; set; }

    public int Compare(Flower x, Flower y)
    {
        if (x.Leaves > y.Leaves)
            return 1;
        if (x.Leaves < y.Leaves)
            return -1;
        else
            return 0;
    }
}

According to MSDN:

Abstract classes are closely related to interfaces. They are classes that cannot be instantiated, and are frequently either partially implemented, or not at all implemented. One key difference between abstract classes and interfaces is that a class may implement an unlimited number of interfaces, but may inherit from only one abstract (or any other kind of) class. A class that is derived from an abstract class may still implement interfaces. Abstract classes are useful when creating components because they allow you specify an invariant level of functionality in some methods, but leave the implementation of other methods until a specific implementation of that class is needed. They also version well, because if additional functionality is needed in derived classes, it can be added to the base class without breaking code.

The key of the above definition is the text that is bold. Since you want classes that has as a base class the Flower class to be compared, this class, Flower has to implement the IComparer<Flower> interface.

Christos
  • 53,228
  • 8
  • 76
  • 108
0

with current code IComparer<Flower> is implemented explicitly (SO: implicit vs explicit implementation)

implement it implicitly to make abstract or virtual:

public abstract class A: IComparer<Flower>
{           
    public abstract int Compare(Flower a, Flower b);
}

OR

public abstract class A: IComparer<Flower>
{           
    public virtual int Compare(Flower a, Flower b)
    {
        if (a.leaves > b.leaves)
            return 1;
        if (a.leaves< b.leaves)
            return -1;
        else
            return 0;
    }
}
Community
  • 1
  • 1
ASh
  • 34,632
  • 9
  • 60
  • 82
  • I have tried with your second option doing this: `List flowers = new List(); A sorting = new A(); flowers.Sort(sorting);` but it won't compile, it says the last line has invalid arguments – moondaisy May 02 '15 at 21:04
  • @moondaisy, if `A` is abstract, it is not allowed to create A object `A sorting = new A();`. But if `B` is inherited from `A` and non-abstract, this shoud work: `A sorting = new B();`; or just make A non-abstract – ASh May 03 '15 at 13:57
  • By bad, I was doing `A sorting = new B()` and it still won't work. – moondaisy May 03 '15 at 15:39
  • Thanks for the demo, I wasn't casting sorting as an `IComparer` – moondaisy May 03 '15 at 22:29