因此,我为此错误画了一个空白。
无法比较数组中的两个元素。
Array.Sort(患者);是产生错误的地方。我确实有一个IComparable接口,以及一个包含以下代码的类文件:尝试按患者ID编号排序

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;
    }
}


这就是我的主要方法。没有添加Display()导致现在没有添加任何内容,为什么将其注释掉

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);

最佳答案

我想到了几件事:

a)为什么不实施IComparable<Patient>

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

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

关于c# - 使用IComparable,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28418413/

10-17 00:45