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