有比较两个整数值的一些问题。做一个的CompareTo是给错误的对象的类型必须为INT32
public interface IData
{
bool EqualsTo(IData otherData);
}
public class IntegerData : IData
{
int _data;
public IntegerData(int data)
{
_data = data;
}
public bool EqualsTo(IData otherData)
{
if(_data.CompareTo(otherData) == 0)
return true;
return false;
}
}
我得到一个错误在该行:
if(_data.CompareTo(otherData) == 0)
说:
Object must of type int32
但在我的观察窗口,我可以看到这两个值是10,并且类型为int 。
怎么了?
我的测试:
IData FirstData = new IntegerData(5);
IData FirstData = new SecondData(5);
bool result = FirstData.Value.EqualsTo(SecondData.Value);
'otherData'是类型的'IData',不'Int'。你可能想要'otherData._Data'。 –