我试图对CustomerArray类中的客户数组进行排序。
客户类通过实现IComparable接口进行默认排序,并按客户ID进行比较。
当尝试使用客户名称对数组进行排序时,它会产生NullException。
如何解决
public class Customer :IComparable
{
public int id;
public string name;
public override string ToString()
{
return string.Format("{0}) {1,7}", id, name);
}
public int CompareTo(object obj)
{
return this.id.CompareTo(((Customer)obj).id);
}
}
public class CustomerArray : IEnumerable,IComparer<Customer>
{
public Customer[] allCustomers;
private int currentSize = 4;
private int CustomerCount = 0;
public CustomerArray() { allCustomers = new Customer[4]; }
public void Add(Customer newCustomer)
{
if (CustomerCount >= allCustomers.Length)
{
Customer[] temp = (Customer[])allCustomers.Clone();
currentSize *= 2;
allCustomers = new Customer[currentSize];
Array.Copy(temp, allCustomers, temp.Length);
temp = null;
}
allCustomers[CustomerCount++] = newCustomer;
}
public void Sort(IComparer<Customer> comp)
{
Array.Sort(this.allCustomers,comp);
}
//public void Sort()
//{
// Array.Sort(allCustomers);
//}
public Customer this[int index]
{
get
{
if (index < 0 || index >= allCustomers.Length)
{
throw new IndexOutOfRangeException();
}
return allCustomers[index];
}
set
{
if (index < 0 || index >= allCustomers.Length)
{
throw new IndexOutOfRangeException();
}
allCustomers[index] = value;
}
}
public Customer this[string name]
{
get
{
foreach (Customer c in allCustomers)
{
if (c.name == name)
return c;
}
throw new IndexOutOfRangeException();
}
set
{
for (int i = 0; i < allCustomers.Length; i++)
{
if (allCustomers[i].name == name)
{
allCustomers[i] = value;
return;
}
}
throw new IndexOutOfRangeException();
}
}
public IEnumerator GetEnumerator()
{
foreach (Customer c in allCustomers)
{
yield return c;
}
}
public int Compare(Customer x, Customer y)
{
return x.name.CompareTo(y.name);
}
}
public class CompareBy : IComparer<Customer>
{
public int Compare(Customer x, Customer y)
{
return x.name.CompareTo(y.name);//when running a NullReferenceException() have thowen why ? and how can i solve it?
}
}
class Program
{
static void Main(string[] args)
{
CustomerArray cs = new CustomerArray();
cs.Add(new Customer { id = 1, name = "khalil" });
cs.Add(new Customer { id = 3, name = "wael" });
cs.Add(new Customer { id = 6, name = "dani" });
cs.Add(new Customer { id = 2, name = "alfred" });
cs.Add(new Customer { id = 4, name = "daad" });
cs.Add(new Customer { id = 5, name = "fadi" });
//
cs.Sort(new CompareBy());//the error is here
//trying to sort using name
foreach (Customer c in cs)
Console.WriteLine(c);
Console.ReadLine();
}
}
public class Customer :IComparable
{
public int id;
public string name;
public override string ToString()
{
return string.Format("{0}) {1,7}", id, name);
}
public int CompareTo(object obj)
{
return this.id.CompareTo(((Customer)obj).id);
}
}
public class CustomerArray : IEnumerable,IComparer<Customer>
{
public Customer[] allCustomers;
private int currentSize = 4;
private int CustomerCount = 0;
public CustomerArray() { allCustomers = new Customer[4]; }
public void Add(Customer newCustomer)
{
if (CustomerCount >= allCustomers.Length)
{
Customer[] temp = (Customer[])allCustomers.Clone();
currentSize *= 2;
allCustomers = new Customer[currentSize];
Array.Copy(temp, allCustomers, temp.Length);
temp = null;
}
allCustomers[CustomerCount++] = newCustomer;
}
public void Sort(IComparer<Customer> comp)
{
Array.Sort(this.allCustomers,comp);
}
//public void Sort()
//{
// Array.Sort(allCustomers);
//}
public Customer this[int index]
{
get
{
if (index < 0 || index >= allCustomers.Length)
{
throw new IndexOutOfRangeException();
}
return allCustomers[index];
}
set
{
if (index < 0 || index >= allCustomers.Length)
{
throw new IndexOutOfRangeException();
}
allCustomers[index] = value;
}
}
public Customer this[string name]
{
get
{
foreach (Customer c in allCustomers)
{
if (c.name == name)
return c;
}
throw new IndexOutOfRangeException();
}
set
{
for (int i = 0; i < allCustomers.Length; i++)
{
if (allCustomers[i].name == name)
{
allCustomers[i] = value;
return;
}
}
throw new IndexOutOfRangeException();
}
}
public IEnumerator GetEnumerator()
{
foreach (Customer c in allCustomers)
{
yield return c;
}
}
public int Compare(Customer x, Customer y)
{
return x.name.CompareTo(y.name);
}
}
public class CompareBy : IComparer<Customer>
{
public int Compare(Customer x, Customer y)
{
return x.name.CompareTo(y.name);//when running a NullReferenceException() have thowen why ? and how can i solve it?
}
}
class Program
{
static void Main(string[] args)
{
CustomerArray cs = new CustomerArray();
cs.Add(new Customer { id = 1, name = "khalil" });
cs.Add(new Customer { id = 3, name = "wael" });
cs.Add(new Customer { id = 6, name = "dani" });
cs.Add(new Customer { id = 2, name = "alfred" });
cs.Add(new Customer { id = 4, name = "daad" });
cs.Add(new Customer { id = 5, name = "fadi" });
//
cs.Sort(new CompareBy());//the error is here
//trying to sort using name
foreach (Customer c in cs)
Console.WriteLine(c);
Console.ReadLine();
}
}
}
&#13;