4

I've changed from using IComparable to IComparable<Artist> However I'm getting the error

'RecordCollection.Artist' does not implement interface member 'System.IComparable.CompareTo(object)'

class Artist : IComparable<Artist>

I've added a CompareTo method.

Not sure what this error means, any help describing why I'm getting this would be great.

class Artist : IComparable<Artist>
{
    private String Name;
    private int NoMem;

    public Artist(string Name, int NoMem)
    {
        this.Name = Name;
        this.NoMem = NoMem; 
    }

 public int CompareTo(Artist other)
    {
        if (other == null) return 1;
        else
            return 0;
    }
}

New Artist AVL tree

        AVLTree<Artist> treeAVL = new AVLTree<Artist>();
  • I assume you mean: I've changed from using IComparable to IComparable ? – Michel Keijzers Mar 15 '13 at 12:17
  • 2
    Also show your CompareTo method ... probably it misses the part. – Michel Keijzers Mar 15 '13 at 12:18
  • 1
    @J.Steen: The actual question (at least mine :-) ) is why it would be required, given that he's only implementing `IComparable`, not `IComparable`. (Note that `IComparable` does not inherit from `IComparable`.) – O. R. Mapper Mar 15 '13 at 12:27
  • @O.R.Mapper Yeah, I know - it only has int CompareTo(T other). Which that method *should* satisfy, so I'm guessing the compiler error is out of whack for some other reason. – J. Steen Mar 15 '13 at 12:28
  • 1
    Please show a minimal class that shows this. For example, did you *replace* the `IComparable` with `IComparable`, or did you *append* `IComparable`. Is `IComparable` requested in another part of a `partial` class, for example. – Marc Gravell Mar 15 '13 at 12:29
  • This is not the answer but will help you what you are missing. If you move your mouse cursor to IComparable it will show a little dialog. It will say implement this interface. You click that then VS will write all the missing functions at the bottom. Go there and write implemenation of these. – fhnaseer Mar 15 '13 at 12:29
  • 5
    @MichelKeijzers there *is* no `` in the implementation; you can't implement an interface method that is non-generic via a generic method – Marc Gravell Mar 15 '13 at 12:30
  • @FaisalHafeez you know the IDE has at least two different "implement interface" options to do this for you, right? – Marc Gravell Mar 15 '13 at 12:31
  • @MarcGravell Yeah it can. But it will help to find out which one is missing. – fhnaseer Mar 15 '13 at 13:34

3 Answers3

3

You have to make sure your project in which you define Artist compiles without errors. Otherwise your other projects won't pick up the change and still think Artist implements IComparable instead of IComparable<T>. That's when you get the compile-time error:

'RecordCollection.Artist' does not implement interface member 'System.IComparable.CompareTo(object)'

There is no technical need to implement CompareTo(object) also, and it won't fix your problem.

Daniel A.A. Pelsmaeker
  • 47,471
  • 20
  • 111
  • 157
1

If you have copied and pasted that error, it looks like you should implement CompareTo like this:

public int CompareTo(object other)
{
    if (other == null) return 1;
        else
    return 0;
}
rhughes
  • 9,257
  • 11
  • 59
  • 87
1

The message:

'RecordCollection.Artist' does not implement interface member 'System.IComparable.CompareTo(object)'

clearly states that it thinks your class still declares that it implements IComparable somewhere. You might want to seek that out (it could be in a different file via partial class). However, personally I think it is correct to include non-typed support. I would simply add:

int IComparable.CompareTo(object obj)
{
    return CompareTo(obj as Artist);
}
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900