使用IComparable

问题描述:

所以我在这个错误上画空白。 无法比较数组中的两个元素。 Array.Sort(patient);是错误产生的地方。我有一个IComparable接口,并用下面的代码类文件:试图通过病人ID号使用IComparable

class Patient : IComparable 
{ 
    private int patientID; 
    private string patientName; 
    private int patientAge; 
    private decimal amount; 

    public int PatientId { get; set; } 

    public string PatientName { get; set; } 

    public int PatientAge { get; set; } 

    public decimal PatientAmount { get; set; } 


    int IComparable.CompareTo(Object o) 
    { 
     int value; 
     Patient temp = (Patient)o; 
     if (this.PatientId > temp.PatientId) 
      value = 1; 
     else if (this.PatientId < temp.PatientId) 
      value = -1; 
     else 
      value = 0; 
     return value; 
    } 
} 

进行排序,这是什么在我的主要方法。没加,现在加给它的显示()因为没有什么,它为什么注释掉

private static void Main(string[] args) 
    { 
     int numOfPatients =2 ; 

     Patient[] patient = new Patient[numOfPatients]; 
     for (int x = 0; x < numOfPatients; x++) 
     { 


      int intvalue; 
      decimal dollarValue; 
      patient[x] = new Patient(); 

      Console.Write("Patient {0}: ", (x + 1)); 
      Console.WriteLine("Enter the Patients ID: "); 
      bool isNum = int.TryParse(Console.ReadLine(), out intvalue); 
      if (isNum) 
      { 
       patient[x].PatientId = intvalue; 

      } 
      else 
      { 
       Console.WriteLine("Patient ID was invalid. ID needs to be numbers"); 
       Console.WriteLine("Enter the Patients ID: "); 
       int.TryParse(Console.ReadLine(), out intvalue); 
      } 

      Console.WriteLine("Enter the Patients Name: "); 
      patient[x].PatientName = Console.ReadLine(); 

      Console.WriteLine("Enter the Patients Age: "); 
      bool isAge = int.TryParse(Console.ReadLine(), out intvalue); 
      if (isAge) 
      { 
       patient[x].PatientAge = intvalue; 

      } 
      else 
      { 
       Console.WriteLine("Patient Age was invalid. Age needs to be numbers"); 
       Console.WriteLine("Enter the Patients Age: "); 
       int.TryParse(Console.ReadLine(), out intvalue); 
      } 

      Console.WriteLine("Enter the Patients Amount Due: "); 
      bool isAmount = Decimal.TryParse(Console.ReadLine(), out dollarValue); 
      if (isAmount) 
      { 
       patient[x].PatientAmount = dollarValue; 

      } 
      else 
      { 
       Console.WriteLine("Patient amount Due was invalid. Amount needs to be a numbers"); 
       Console.WriteLine("Enter the Patients Amount Due: "); 
       int.TryParse(Console.ReadLine(), out intvalue); 
      } 


     } 
     Array.Sort(patient); 
     Console.WriteLine("Patients in order with Amounts Owed are: "); 
     for (int i = 0; i < patient.Length; ++i) ; 
     //Display(patient[i], numOfPatients); 
+2

你有什么错误?这听起来不像标准.NET异常 – BradleyDotNET 2015-02-09 20:12:15

+1

在mscorlib.dll中发生未处理的异常'System.InvalidOperationException' 附加信息:未能比较数组中的两个元素。出现的错误是 – MeggMercer 2015-02-09 20:14:06

+3

您不会在比较方法中处理NULL和对象而不是您的类。 – Dawnkeeper 2015-02-09 20:15:42

有几件事情浮现在脑海中:

一)为什么不执行IComparable<Patient>

b)为什么要重新执行int.CompareTo(int)? IComparable的实现可能会返回this.PatientID.CompareTo(other.PatientID)

c)您是否确定排序时阵列已满?我不确定如果它包含null会发生什么。

+1

谢谢我做了'IComparable '的方式,不得不使用'set {accessor-body}'并且错误停止了 – MeggMercer 2015-02-09 20:31:59

我只想写

return this.PatientId.CompareTo(temp.PatientId) 

的重写CompareTo方法内部类。不需要使用平等符号。这将为你做int比较并返回正确的值。

我也建议你只使用一些IList类的实现,然后你可以使用LinQ语句。使用这将防止曾经有过的“阵列”中是一个空值

它看起来像是Array.Sort传递一个类型数组会调用Array.Sort<T>(T[])过载。根据MSDN documentation,此过载使用接口来比较对象。因此,它看起来就像你有两个选择:

  1. 您可以实现IComparable<T>而不是IComparable(更好)。
  2. 你可以投你的阵列Array调用Array.Sort(Array)超载它使用IComparable接口(差)。