2010-10-22 109 views
0

我有我從Java轉換的C#代碼。在此,我得到的錯誤「未實現inteface成員System.Collections.IComparer.Comparer(對象,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; 
     } 

感謝。

+0

C#is case sens itive - 將'compare'更改爲'Compare'。 – RPM1984 2010-10-22 04:37:59

回答

2

的IComparer有一個方法:

int Compare(
    object x, 
    object y 
); 

它看起來像你需要大寫C對 「比較」

+0

謝謝...錯誤消失了。 – user437890 2010-10-22 04:50:38

0

您需要更改線路

public virtual int compare(object o1, object o2) 

public virtual int Compare(object o1, object o2) 

C#是c ASE敏感...

+0

非常感謝。現在錯誤消失了 – user437890 2010-10-22 04:50:10

0

我幾乎同樣的問題,但我的解決辦法是向公衆修飾符添加到我創建的方法:

問題:

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

在這兩種情況下,我和你的,我想這只是一個注意問題...