我有一個產品類別:我想對列表進行排序,但它不工作
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解決它?
這是怎麼回事?是否有可能你的'電子郵件'值爲空? – 2012-08-17 18:12:20
哪一行?任何堆棧跟蹤? – 2012-08-17 18:13:28