无法在 Silverlight 上的列表中找到项目的索引
我有一个“国家/地区”类型的项目列表,我正在尝试查找列表中特定国家/地区的索引,但 IndexOf() 方法始终返回 -1。
Country 对象如下所示:
public class Country
{
public string CountryCode { get; set; }
public string CountryName { get; set; }
}
然后,当我尝试使用 IndexOf() 方法时,我会执行以下操作:
var newcountry = new Country
{
CountryCode = "VE",
CountryName = "VENEZUELA"
};
var countries = ListBoxCountries.Items.Cast<Country>().ToList();
if (countries.IndexOf(newcountry) == -1)
countries.Add(newcountry);
假设我有一个已填充国家/地区的列表,并且“委内瑞拉”在列表中,则 IndexOf() 方法永远不会找到国家。
编辑:
所以我在这里得到了 ReSharper 的一点帮助,当我告诉他重写 Equals() 方法时,他做了这个:
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != typeof (Country)) return false;
return Equals((Country) obj);
}
public bool Equals(Country other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return Equals(other.CountryCode, CountryCode) && Equals(other.CountryName, CountryName);
}
public override int GetHashCode()
{
unchecked
{
return ((CountryCode != null ? CountryCode.GetHashCode() : 0)*397) ^ (CountryName != null ? CountryName.GetHashCode() : 0);
}
}
这里又出现了一个问题:做这一切只是为了比较两个对象可以吗?
I have a list of items of type Country and I'm trying to find the index of and specific Country on the list but the IndexOf() method always returns -1.
The Country object look like this:
public class Country
{
public string CountryCode { get; set; }
public string CountryName { get; set; }
}
Then when I try to use the IndexOf() method I do the next:
var newcountry = new Country
{
CountryCode = "VE",
CountryName = "VENEZUELA"
};
var countries = ListBoxCountries.Items.Cast<Country>().ToList();
if (countries.IndexOf(newcountry) == -1)
countries.Add(newcountry);
Lets assume I have an already filled list with with the countries and "Venezuela" is on the list, the IndexOf() method never find the country.
EDIT:
So I got a little help from ReSharper here and he made this once I told him to override the Equals() method:
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != typeof (Country)) return false;
return Equals((Country) obj);
}
public bool Equals(Country other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return Equals(other.CountryCode, CountryCode) && Equals(other.CountryName, CountryName);
}
public override int GetHashCode()
{
unchecked
{
return ((CountryCode != null ? CountryCode.GetHashCode() : 0)*397) ^ (CountryName != null ? CountryName.GetHashCode() : 0);
}
}
And here comes another question: It's ok to do all this just to compare two objects?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我怀疑这是由于参考问题造成的。您需要重写
Country
类中的Equals();
方法进行检查。我会使用这样的代码:
I suspect this is due to a reference issue. You'll need to override the
Equals();
method in yourCountry
class to check.I'd use code like this:
那是因为 IndexOf 使用引用相等来比较对象
您可以使用这个
或者您可以更好地使用 ovverride Equals() 方法来比较对象。
That's because IndexOf uses reference equality for comparing objects
You can use this
Or you can better ovverride Equals() method to compare objects.