简体   繁体   English

IComparable <T>不实现

[英]IComparable<T> Does not Implement

I've changed from using IComparable to IComparable<Artist> However I'm getting the error 我已经从使用IComparable改为IComparable<Artist>但是我收到了错误

'RecordCollection.Artist' does not implement interface member 'System.IComparable.CompareTo(object)' 'RecordCollection.Artist'没有实现接口成员'System.IComparable.CompareTo(object)'

class Artist : IComparable<Artist>

I've added a CompareTo method. 我添加了一个CompareTo方法。

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 新艺术家AVL树

        AVLTree<Artist> treeAVL = new AVLTree<Artist>();

You have to make sure your project in which you define Artist compiles without errors. 您必须确保您在其中定义Artist项目编译没有错误。 Otherwise your other projects won't pick up the change and still think Artist implements IComparable instead of IComparable<T> . 否则你的其他项目将不会接受更改,仍然认为Artist实现IComparable而不是IComparable<T> That's when you get the compile-time error: 那是你得到编译时错误的时候:

'RecordCollection.Artist' does not implement interface member 'System.IComparable.CompareTo(object)' 'RecordCollection.Artist'没有实现接口成员'System.IComparable.CompareTo(object)'

There is no technical need to implement CompareTo(object) also, and it won't fix your problem. 也没有技术上需要实现CompareTo(object) ,它也无法解决您的问题。

If you have copied and pasted that error, it looks like you should implement CompareTo like this: 如果您复制并粘贴了该错误,看起来您应该像这样实现CompareTo

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)' 'RecordCollection.Artist'没有实现接口成员'System.IComparable.CompareTo(object)'

clearly states that it thinks your class still declares that it implements IComparable somewhere. 明确表示它认为你的类仍然声明它在某处实现了IComparable You might want to seek that out (it could be in a different file via partial class ). 您可能想要寻找它(它可能通过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 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM