을 작동하지 않습니다 : 나는나는 목록을 정렬하고 싶지만 내가 한 제품 클래스가
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");
}
}
}
그런 다음 ICompare 일 오전 또 하나의 클래스를 만들
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;
}
}
를 내가 양식에서 제품 목록 클래스에서 전화가
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();
}
하지만 나에게 널 (null) 예외를 보여
개체 참조가 개체의 인스턴스로 설정되지 않았습니다 ** 당신이 me.How이 그것을 해결하기 위해 도와주세요 수 있습니다.?
어떤 문제가 발생합니까? 'Email' 값 중 하나가 null 일 수 있습니까? –
어느 라인입니까? 어떤 stacktrace? –