سؤال

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>();
هل كانت مفيدة؟

المحلول

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.

نصائح أخرى

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;
}

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);
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top