2013-12-12 39 views
2

所以這似乎是一個合理的數額(基於同一題目的問題),但我很難倒我的事。 我繼承的代碼,我真的很困惑的我越來越「給定的密鑰不在字典中」 - 但它是

錯誤我有一個對象

FiscalSamplingPoint currentPoint 

非常基本的定義

public sealed class FiscalSamplingPoint : SamplingPoint, IComparable<FiscalSamplingPoint>, IEquatable<FiscalSamplingPoint> 
{ 
    private readonly int _fiscalYear; 
    private readonly int _periodNumber; 
    private readonly FiscalPeriodType _periodType; 

... 
} 

FiscalPeriodType是一個枚舉

我有一個SortedList(不確定他們爲什麼將它用作字典)關鍵FiscalSamplingPoint的「絕對值」。

SortedList<FiscalSamplingPoint, PeriodsRepositoryRange> 

在一個特定的當前點就拋出指定鍵不存在異常,併爲例外,

absolutes.containsKey(currentPoint) 

是假的(Visual Studio 2010的手錶調試器)

然而,我們已經重寫的GetHashCode和等於。

,並再次使用Visual Studio的手錶調試我做

currentPoint.GetHashCode 

(new System.Collections.Generic.System_DictionaryDebugView<TR.API.Common.FiscalSamplingPoint,TR.API.Common.PeriodsRepositoryRange>(absolutes)).Items[3].Key.GetHashCode() 

兩者返回相同的結果(62234確切地說)

currentPoint.Equals((new System.Collections.Generic.System_DictionaryDebugView<TR.API.Common.FiscalSamplingPoint,TR.API.Common.PeriodsRepositoryRange>(absolutes)).Items[3].Key) 

返回true。

我真的不知道這個問題是什麼。任何指針將不勝感激。

編輯:這裏是相關職能

public bool Equals(FiscalSamplingPoint obj) 
{ 
    if (ReferenceEquals(null, obj)) return false; 
    if (ReferenceEquals(this, obj)) return true; 
    return InternalEquals(obj); 
} 

public override bool Equals(object obj) 
{ 
    if (ReferenceEquals(null, obj)) return false; 
    if (ReferenceEquals(this, obj)) return true; 
    if (obj.GetType() != typeof (FiscalSamplingPoint)) return false; 
     return InternalEquals((FiscalSamplingPoint) obj); 
} 

private bool InternalEquals(FiscalSamplingPoint obj) 
{ 
    return _fiscalYear == obj.FiscalYear && _periodNumber == obj.PeriodNumber && _periodType == obj.PeriodType; 
} 

public override int GetHashCode() 
{ 
    return 31*_fiscalYear + 53*_periodNumber + 13*(int) _periodType; 
} 

對於IComparable的,我們有一起工作的

public int CompareTo(FiscalSamplingPoint other) 
    { 
     if (other == null) 
     { 
      throw new ApplicationException("Other object is null"); 
     } 

     if (this > other) 
     { 
      return 1; 
     } 
     if (Equals(other)) 
     { 
      return 0; 
     } 
     return -1; 
    } 

    public static bool operator <(FiscalSamplingPoint point1, FiscalSamplingPoint point2) 
    { 
     if (point1._periodType < point2._periodType) 
      return true; 

     if (point1._fiscalYear < point2._fiscalYear) 
      return true; 

     if (point1._fiscalYear == point2._fiscalYear) 
      return point1.PeriodNumber < point2.PeriodNumber; 

     return false; 
    } 

    public static bool operator >(FiscalSamplingPoint point1, FiscalSamplingPoint point2) 
    { 
     if (point1._periodType > point2._periodType) 
      return true; 

     if (point1._fiscalYear > point2._fiscalYear) 
     { 
      return true; 
     } 
     if (point1._fiscalYear == point2._fiscalYear) 
     { 
      return point1.PeriodNumber > point2.PeriodNumber; 
     } 

     return false; 
    } 
+3

我不能完全告訴那裏的代碼開始,並在對話結束 –

+3

如果你能發佈[SSCCE(http://www.sscce.org/),比將有很大的幫助 –

+1

必須說明你是如何覆蓋平等的。這是這裏的主要嫌疑人。 – NoChance

回答

2

<>運營商實現一些功能的原因是,absolutes.containsKey(currentPoint)返回false即使鑰匙出現在列表中。 SordedList<T,K>使用BinarySearch在列表中查找關鍵字,但找不到它,因爲列表未正確排序。

Enum值很可能在不正確的比較中起主要作用。他們的數值大於,甚至認爲語境意義可能更小。

我懷疑你可以將<>運營商更改爲類似Will在他的回答中所暗示的內容。

+0

你說得對,< and >的實現是錯誤的,但你的錯誤也是錯誤的。當類型和年份匹配時,它會錯誤地返回false,但週期<。 – Will

+0

您是對的嗎,先生。我寫這篇文章時似乎很合理。你的解決方案絕對更清潔。 –

+0

沒有probs,希望OP能夠從我們的共同努力中獲得他所需要的。 – Will

2

錯誤在於執行<和>操作符,它們有時會返回不正確的真/假值。如果CompareTo按以下方式實現,則不需要它們。

ps。如果您需要按不同順序排序,請將類型,年份和週期順序交換。

public int CompareTo(FiscalSamplingPoint point) 
{ 
    if (point == null) 
     return 1; 

    if (this._periodType != point._periodType) 
     return this._periodType < point._periodType? -1 : 1; 

    if (this._fiscalYear != point._fiscalYear) 
     return this._fiscalYear < point._fiscalYear? -1 : 1; 

    if (this._periodNumber != point._periodNumber) 
     return this._periodNumber < point._periodNumber? -1 : 1; 

    return 0; 
} 

NB我還用的CompareTo爲Equals執行,以避免重複的代碼。例如。

point1.CompareTo(point2) == 0 
相關問題