방금 입력 한 키가 존재하지 않는다는 나의 사전이 왜입니까? 내 Equals
방법 또는 이중 비교와 관련이 있습니까?존재할 때 사전 키를 찾을 수 없음
// Test: I know the dictionary contains nCoord but its saying the key doesn't exist
Dictionary<UTMCoordinate, int> planes = new Dictionary<UTMCoordinate, int>();
UTMCoordinate nCoord = new UTMCoordinate(337394.136407966, 6263820.40182064, 0, 56, UTMCoordinate.Hemisphere.H_SOUTHERN);
planes[nCoord] = 1;
bool exists = planes.ContainsKey(nCoord); // always returns false
UTMCoordinate
의 내 구현은 다음과 같습니다 :
public class UTMCoordinate
{
public enum Hemisphere {H_NOTHERN, H_SOUTHERN};
public const double DIF_TOLERANCE = 0.0005;
public double x { get; set; }
public double y { get; set; }
public double elev { get; set; }
public uint UTMZone { get; set; }
public Hemisphere hemisphere { get; set; }
public UTMCoordinate(double x, double y, double elev=double.MinValue, uint utmZone=uint.MinValue, Hemisphere hemisphere=Hemisphere.H_SOUTHERN) {
this.x = x;
this.y = y;
this.elev = elev;
this.UTMZone = utmZone;
this.hemisphere = hemisphere;
}
public override int GetHashCode() {
unchecked // Overflow is fine, just wrap
{
int hash = 17;
// Suitable nullity checks etc, of course :)
hash = hash * 23 + x.GetHashCode();
hash = hash * 23 + y.GetHashCode();
hash = hash * 23 + elev.GetHashCode();
hash = hash * 23 + UTMZone.GetHashCode();
hash = hash * 23 + hemisphere.GetHashCode();
return hash;
}
}
public override bool Equals(object obj)
{
UTMCoordinate other = obj as UTMCoordinate;
if (other == null)
return false;
return double.Equals(x, other.x) && double.Equals(y, other.y) && double.Equals(elev, other.elev) && uint.Equals(UTMZone, other.UTMZone) && double.Equals(hemisphere, other.hemisphere);
}
}
편집 내가 다른 더블을 비교 한 방법을 사용했습니다 다니엘 A. 백상 조언을 사용하여 다음
는 코드입니다. 나는 귀하의 질문에서 코드를 가지고가는 경우에public override bool Equals(object obj)
{
//return base.Equals (obj);
UTMCoordinate other = obj as UTMCoordinate;
if (other == null)
return false;
//return double.Equals(x, other.x) && double.Equals(y, other.y) && double.Equals(elev, other.elev) && uint.Equals(UTMZone, other.UTMZone) && double.Equals(hemisphere, other.hemisphere);
return Math.Abs (x-other.x) <= DIF_TOLERANCE && Math.Abs (y-other.y) <= DIF_TOLERANCE && Math.Abs (elev-other.elev) <= DIF_TOLERANCE && uint.Equals(UTMZone, other.UTMZone) && hemisphere == other.hemisphere;
}
fyi double에는 고정 된 정밀도가 없습니다. 그것은 당신에게 단서를 줄 수 있습니다. –
두 가지 방법 모두에 중단 점을 지정하십시오. 그들이 기대할 때 그들이 부름을 받는가? 그들은 당신이 기대하는 가치를 반환합니까? –
방금 시도했습니다. 'planes.ContainsKey (nCoord)'는 나에게'true'를 reture했다. – AlexD