所以這似乎是一個合理的數額(基於同一題目的問題),但我很難倒我的事。 我繼承的代碼,我真的很困惑的我越來越「給定的密鑰不在字典中」 - 但它是
錯誤我有一個對象
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;
}
我不能完全告訴那裏的代碼開始,並在對話結束 –
如果你能發佈[SSCCE(http://www.sscce.org/),比將有很大的幫助 –
必須說明你是如何覆蓋平等的。這是這裏的主要嫌疑人。 – NoChance