执行 CompareTo 会给出错误 Object must be of type int32
比较 2 个整数值时遇到一些问题。
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);
Having some issues comparing 2 integer values.
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;
}
}
I'm getting an error in the line:
if(_data.CompareTo(otherData) == 0)
Saying:
Object must of type int32
But in my watch window I can see both values are 10, and of Type int.
What's wrong?
My test:
IData FirstData = new IntegerData(5);
IData FirstData = new SecondData(5);
bool result = FirstData.Value.EqualsTo(SecondData.Value);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我会尽量简单:
这就是你正在做的:
这就是你需要的:
现在,这是你可以做到的(非常简单的方式):
这是实现相同目标的另一种方法(我将其用于你的场景):
这是同一任务的另一个解决方案:
- 请记住,这是针对一个较小问题的更大解决方案。这可能会导致更大的类和更大的问题,因此仅在需要时才使用它。简单点。保持简单。不要陷入太复杂的事情,因为随着时间的推移,您将不得不维护该代码...
- 另请记住,我使用了默认的“GetHashCode”方法。有时这就足够了,但请记住,您可能需要根据您的需要创建/使用自定义哈希算法。
- 最后考虑一下这只是一个例子。我已经根据加布的答案创建了接口,但添加了一个仅用于哈希本身的方法。您可能想要删除或改进。考虑您的需求。
I'll try to be as simple as possible:
This is what you are doing:
This is what you need:
Now, here is how you can do it (very simplistic way):
This is another way to achieve the same (which I'd use for your scenario):
And this is another solution for the same task:
- Remember that this is a bigger solution for a somewhat small problem. This could lead to bigger classes and bigger problems so use it only if you need it. Be simple. Keep simple. Don't get into something too complex because you'll have to maintain that code over time...
- Also keep in mind that I've used the default "GetHashCode" method. Sometimes that's enough but keep in mind that you might need to create/use a custom hash algorithm depending on your need.
- Finally consider that this is just an example. I've created the interfaced based on Gabe's answer but added a method just for the hash itself. You might want to remove or improve. Consider your needs.
您正在将一个整数与引发异常的行中的 IData 对象进行比较。您需要将
IData
对象转换为IntegerData
对象,然后将其_data
属性与本地_data
属性进行比较。一种更优雅的方法是使用泛型来处理不同类型的情况:
You are comparing an integer to an
IData
object in the line that is throwing the exception. You need to cast theIData
object to anIntegerData
object and then compare it's_data
property with the local_data
property.A more elegant approach is to use generics to handle your varying type cases:
otherData
的类型为IData
您必须添加一个 getter 来获取
int
属性,因此对于interface
我添加GetData()
方法然后在 IntegerData 中实现它,
如下所示:
otherData
is of typeIData
You would have to add a getter to get your
int
property so for theinterface
I add aGetData()
methodthen implemented it in
IntegerData
Something like this: