我想对列表进行排序,但它不工作
我有一个产品类别:我想对列表进行排序,但它不工作
public class Product
{
private string firstname;
private string lastname;
private string email;
public Product()
{
}
public Product(string firstname, string lastname, string email)
{
this.Firstname = firstname;
this.Lastname = lastname;
this.Email = email;
}
public string Firstname
{
get
{
return firstname;
}
set
{
firstname = value;
}
}
public string Lastname
{
get
{
return lastname;
}
set
{
lastname = value;
}
}
public string Email
{
get
{
return email;
}
set
{
email = value;
}
}
public virtual string GetDisplayText(string sep)
{
return Firstname + sep + Lastname + sep + Email;
}
}
我做出一个更课堂,我做了ICompare
public class PersonSort : IComparer<Product>
{
public enum CompareType
{
Email
}
private CompareType compareType;
public PersonSort(CompareType cType)
{
this.compareType = cType;
}
public int Compare(Product x, Product y)
{
if (x == null) throw new ArgumentNullException("x");
if (y == null) throw new ArgumentNullException("y");
int result;
switch (compareType)
{
case CompareType.Email:
return x.Email.CompareTo(y.Email);
default:
throw new ArgumentNullException("Invalid Compare Type");
}
}
}
然后我在产品列表级呼叫
List<Product> person;
public void Sort()
{
person.Sort(new PersonSort(PersonSort.CompareType.Email));
}
然后,在形成了本方法调用:
private ProductList products = new ProductList();
private void button4_Click(object sender, EventArgs e)
{
products.Sort();
}
,但它告诉我空例外:对象引用不设置到对象的实例** 能否请你帮me.How解决它?
List<Product> person;
这哪里是给定的值?您没有在代码中加入person
列表并添加项目(或将项目添加到列表中,然后将其分配给person
等)。这可能会导致问题。
public int Compare(Product x, Product y)
{
if (x == null) throw new ArgumentNullException("x");
if (y == null) throw new ArgumentNullException("y");
这是一个坏主意,因为它是IComparer<T>.Compare
的文档,这是正常的空传递的一部分,空参数计算为小于其他任何说法。虽然我不认为它与List<T>.Sort()
一起使用,但它仍然是使用比较器的方法可以依赖于传入null是安全的。因此:
public int Compare(Product x, Product y)
{
if(ReferenceEquals(x, y))//either both null or both the same instance
return 0;
if(x == null)
return -1;
if(y == null)
return 1;
这可能是相关的。
最后,如果Email
场为null,则可能会引发在
return x.Email.CompareTo(y.Email)
最好的办法就是在构造函数和setter代码,以便它是不可能的,要不会发生。删除无参数构造函数,向其他构造函数和检查器添加一个空检查,以便在抛出异常时创建一个伪造Product
而不是稍后的抛出ArgumentNullException
。
您还可以添加一个检查到比较器:
if(x.Email == null || y.Email == null)
throw new Exception("Cannot compare a user with null email");
,不会修复bug,但将帮助您跟踪下来。
根据提供的代码,person
in ProductList
未初始化。话虽如此,如果您在问题中包含异常的调用堆栈,您将得到明确的答案。
List<Product> person;
到
List<Product> person = new List<Product>();
它仍然不起作用,即使它不给我任何错误 – user1050667 2012-08-17 18:27:25
这是否解决了NullReferenceException? “仍然行不通”对我来说并不重要。 – 2012-08-17 19:11:42
你有null
参考的地方。确保列表已初始化。另外,是否正确设置Product.Email
?
您可能想用StringComparer
来代替。更换
return x.Email.CompareTo(y.Email);
与
return StringComparer.Ordinal.Compare(x.Email, y.Email);
如何传递电子邮件中的值,因为它已经在XML文件中,并且我在Listbox中上传了这些值,然后我想对该列表进行排序? – user1050667 2012-08-17 18:36:07
@ user1050667,抱歉,但我不明白你的设置。什么XML文件?通过它在哪里?什么列表框?只需提出专门针对单个问题的新问题,并在成功解决特定主题时接受答案。 – Lucero 2012-08-17 23:23:11
这是怎么回事?是否有可能你的'电子邮件'值为空? – 2012-08-17 18:12:20
哪一行?任何堆栈跟踪? – 2012-08-17 18:13:28