不实现接口错误

时间:2010-10-22 04:34:47

标签: c#-4.0

我有以下从Java转换的C#代码。在这我得到错误“没有实现inteface成员System.Collections.IComparer.Comparer(object,obkject)”

请帮我解决此错误

internal class CompositeComparator : IComparer
{
    private IComparer major;
    private IComparer minor;
    public CompositeComparator(IComparer major, IComparer minor)
    {
        this.major = major;
        this.minor = minor;
    }
    public virtual int compare(object o1, object o2)
    {
        int result = major.Compare(o1, o2);
        if (result != 0)
        {
            return result;
        }
        else
        {
            return minor.Compare(o1, o2);
        }
    }
}

internal class ComparatorIStep : IComparer
{
    public ComparatorIStep()
    {
    }
    public virtual int compare(object o1, object o2)
    {
        int nReturn = ((LogMessage)o1).IStep - ((LogMessage)o2).IStep;
        return nReturn;
    }
}

internal class ComparatorType : IComparer
{
    public ComparatorType()
    {
    }
    public virtual int compare(object o1, object o2)
    {
        int nReturn = 0;

        LogMessage lm1 = (LogMessage)o1;
        LogMessage lm2 = (LogMessage)o2;
        bool b1 = lm1.isHeading;
        bool b2 = lm2.isHeading;
        if (b1 || !b2)
        {
            nReturn = -1;
        }
        else if (!b1 || b2)
        {
            nReturn = 1;
        }

感谢。

3 个答案:

答案 0 :(得分:2)

IComparer有一种方法:

int Compare(
   object x,
   object y
);

看起来你需要在“比较”

上大写c

答案 1 :(得分:0)

您需要更改行

public virtual int compare(object o1, object o2)

public virtual int Compare(object o1, object o2)

C#区分大小写...

答案 2 :(得分:0)

我遇到了几乎相同的问题,但我的解决方案是将public修饰符添加到我创建的方法中:

问题:

public class Ordenacao : IComparer<tab>
{
    int Compare(tab x, tab y)
    {
        tab a = (tab) x;
        tab b = (tab) y;
        return a.ordinal.CompareTo(b.ordinal);
    }
}

解决方案:

public class Ordenacao : IComparer<tab>
{
    public int Compare(tab x, tab y)
    {
        tab a = (tab) x;
        tab b = (tab) y;
        return a.ordinal.CompareTo(b.ordinal);
    }
}

在这两种情况下,我和你的,我想这只是一个关注问题...