在有人指出this post之前,我已经读过它而没有运气。
我有一个LatLon类,它扩展了Bing.Maps的Location类(documentation HERE),主要用于覆盖GetHashCode和Equals方法以执行值相等而不是内置引用相等,如下所示:
class LatLon : Location
{
public readonly double Latitude;
public readonly double Longitude;
public LatLon(double Latitude, double Longitude)
{
this.Latitude = Latitude;
this.Longitude = Longitude;
}
public override bool Equals(System.Object obj)
{
// If parameter is null return false.
if (obj == null)
{
return false;
}
// If parameter cannot be cast to Point return false.
LatLon p = obj as LatLon;
if ((System.Object)p == null)
{
return false;
}
// Return true if the fields match:
return (Latitude == p.Latitude) && (Longitude == p.Longitude);
}
public bool Equals(LatLon p)
{
// If parameter is null return false:
if ((object)p == null)
{
return false;
}
// Return true if the fields match:
return (Latitude == p.Latitude) && (Longitude == p.Longitude);
}
public override int GetHashCode()
{
int hash = 13;
hash = (hash * 92821) + Latitude.GetHashCode();
hash = (hash * 92821) + Longitude.GetHashCode();
return hash;
}
}
在另一堂课中,我正在努力做到以下几点:
LatLon l1 = (LatLon) getBinnedLocation(new LatLon(41, -51.000001));
...
private Location getBinnedLocation(Location loc)
{
return new Location(
getBinnedCoord(loc.Latitude, true),
getBinnedCoord(loc.Longitude, false));
}
我是来自Java的C#的新手,但我的理解是继承的原则基本保持不变。那么为什么行LatLon l1 = (LatLon) getBinnedLocation(new LatLon(41, -51.000001));
会导致错误:The best overloaded method match for 'SomeClass.getBinnedLocation(Bing.Maps.Location)' has some invalid arguments
。
使用非常简单的解决方案似乎是一个非常简单的问题,但我无法弄清楚出了什么问题。
编辑:
证明我的位置指的是Bing.Maps的位置,而不是我名称空间中的另一个位置类:http://i.imgur.com/YHBVllT.png。
错误图片:http://i.imgur.com/tM2bf53.png。
Error 3 'object' does not contain a constructor that takes 2 arguments C:\...\LatLon.cs 16 15 MapTest Error 4 The name 'Latitude' does not exist in the current context C:\...\LatLon.cs 36 21 MapTest Error 5 'MapTest.LatLon' does not contain a definition for 'Latitude' and no extension method 'Latitude' accepting a first argument of type 'MapTest.LatLon' could be found (are you missing a using directive or an assembly reference?) C:\...\LatLon.cs 36 35 MapTest Error 6 The name 'Longitude' does not exist in the current context C:\...\LatLon.cs 36 49 MapTest Error 7 'MapTest.LatLon' does not contain a definition for 'Longitude' and no extension method 'Longitude' accepting a first argument of type 'MapTest.LatLon' could be found (are you missing a using directive or an assembly reference?) C:\...\LatLon.cs 36 64 MapTest Error 8 The name 'Latitude' does not exist in the current context C:\...\LatLon.cs 48 21 MapTest Error 9 'MapTest.LatLon' does not contain a definition for 'Latitude' and no extension method 'Latitude' accepting a first argument of type 'MapTest.LatLon' could be found (are you missing a using directive or an assembly reference?) C:\...\LatLon.cs 48 35 MapTest Error 10 The name 'Longitude' does not exist in the current context C:\...\LatLon.cs 48 49 MapTest Error 11 'MapTest.LatLon' does not contain a definition for 'Longitude' and no extension method 'Longitude' accepting a first argument of type 'MapTest.LatLon' could be found (are you missing a using directive or an assembly reference?) C:\...\LatLon.cs 48 64 MapTest Error 12 The name 'Latitude' does not exist in the current context C:\...\LatLon.cs 54 37 MapTest Error 13 The name 'Longitude' does not exist in the current context C:\...\LatLon.cs 55 37 MapTest Error 14 The best overloaded method match for 'MapTest.MainPage.getBinnedLocation(Bing.Maps.Location)' has some invalid arguments C:\...\MainPage.xaml.cs 75 25 MapTest Error 15 Argument 1: cannot convert from 'MapTest.LatLon' to 'Bing.Maps.Location' C:\...\MainPage.xaml.cs 75 43 MapTest Error 16 The best overloaded method match for 'MapTest.MainPage.getBinnedLocation(Bing.Maps.Location)' has some invalid arguments C:\...\MainPage.xaml.cs 76 25 MapTest Error 17 Argument 1: cannot convert from 'MapTest.LatLon' to 'Bing.Maps.Location' C:\...\MainPage.xaml.cs 76 43 MapTest Error 18 'MapTest.LatLon' does not contain a definition for 'Latitude' and no extension method 'Latitude' accepting a first argument of type 'MapTest.LatLon' could be found (are you missing a using directive or an assembly reference?) C:\...\MainPage.xaml.cs 79 40 MapTest Error 19 'MapTest.LatLon' does not contain a definition for 'Longitude' and no extension method 'Longitude' accepting a first argument of type 'MapTest.LatLon' could be found (are you missing a using directive or an assembly reference?) C:\...\MainPage.xaml.cs 79 61 MapTest Error 20 'MapTest.LatLon' does not contain a definition for 'Latitude' and no extension method 'Latitude' accepting a first argument of type 'MapTest.LatLon' could be found (are you missing a using directive or an assembly reference?) C:\...\MainPage.xaml.cs 80 40 MapTest Error 21 'MapTest.LatLon' does not contain a definition for 'Longitude' and no extension method 'Longitude' accepting a first argument of type 'MapTest.LatLon' could be found (are you missing a using directive or an assembly reference?) C:\...\MainPage.xaml.cs 80 61 MapTest
答案 0 :(得分:2)
您的LatLon课程可能会出现一些问题,可能会导致您的问题。看看修复这些问题是否能修复您的代码。
base(
调用基础类' es consturctor。getBinnedLocation
返回new Location
,但您将其转换为LatLon
,这将在运行时失败。只需致电new LatLon
并让其返回LatLon
而不是Location
。PascalCased
而不是camelCased
class LatLon : Location
{
//Get rid of these
//public readonly double Latitude;
//public readonly double Longitude;
public LatLon(double latitude, double longitude)
: base(latitude, longitude) //This calls the base's (double, double) constuctor.
{
}
//... Everything else can stay the same
}
LatLon l1 = GetBinnedLocation(new LatLon(41, -51.000001));
...
private LatLon GetBinnedLocation(Location loc)
{
return new LatLon(
getBinnedCoord(loc.Latitude, true),
getBinnedCoord(loc.Longitude, false));
}
<强>更新强>
感谢您更新的问题,确实您遇到问题的原因(错误15)是错误1-14,导致您的课程无法编译。修复1-14将修复您的错误。
从你的描述中可以看出VS对这种类型感到困惑。它的行为就好像Location不是一个定义的类型。尝试删除定义类型的dll引用并重新添加它。
答案 1 :(得分:-2)
将LatLon实例发送到位置的函数时,将其转换为位置。