繁体   English   中英

一个类的两个实例:仅排序1

[英]Two instances of a class: only sort 1

我有一个具有四个属性的基本类( Patient )。 Main()我为数组保留了内存,在实际创建实例之前,我向用户询问了帐号,以确保该数组中不存在该帐号。 因此BinarySearch要求对它进行排序,但是一旦我对它进行排序,就会失去执行for循环的能力。

//Variables
int intMaxNum = 5; //set max number of patients to 5
int intInputValue;
int intResult;
string strTempName;
int intTempAge;
double dblTempTotal;

Patient[] objectPatient = new Patient[intMaxNum]; //create an array of references

for (int x = 0; x < objectPatient.Length; ++x)
{
   //attempt to create a 'shadow' class to search through and keep integrity of main class (objectPatient)
   Patient[] tempobjectPatient = new Patient[intMaxNum];
   tempobjectPatient = objectPatient;
   if (x > 0)
   {

      Console.Write("\n***Next Patient***");

      Array.Sort(tempobjectPatient); //this will sort both objects even though I send the temporary class only - interface impact I'm sure
   }

   //ask for the Patient Account number
   Console.Write("\nEnter Patient Account Number: ");
   ReadTheAccountNumber:
   intInputValue = Convert.ToInt32(Console.ReadLine());
   //create temporary class for comparison
   Patient SeekPatient = new Patient();
   SeekPatient.PatientNumber=intInputValue; // reset the default info with the input Pateint Account Number

   //verify the Patient Account number doesn't already exist
   intResult = Array.BinarySearch(tempobjectPatient, SeekPatient);
   //intResult = Array.BinarySearch(objectPatient, SeekPatient);

   //if (objectPatient.Equals(SeekPatient)) //Can not get the .Equals to work at all...
   if (intResult >= 0)
   {
      Console.Write("\nSorry, Patient Account Number {0} is a duplicate.", intInputValue);
      Console.Write("\nPlease re-enter the Patient Account Number: ");
      goto ReadTheAccountNumber;
   }
   else //no match found, get the rest of the data and create the object
   {
      if (x > 0) { Console.Write("***Patient Account Number unique and accepted***\n"); } //looks silly to display this if entering the first record
      Console.Write("Enter the Patient Name: ");
      strTempName = Console.ReadLine();
      Console.Write("Enter the Patient Age: ");
      intTempAge = Convert.ToInt32(Console.ReadLine());
      Console.Write("Enter the total annual Patient amount due: ");
      dblTempTotal = Convert.ToDouble(Console.ReadLine());
      objectPatient[x] = new Patient(intInputValue, strTempName, intTempAge, dblTempTotal);
   }
}

这是课程:

class Patient : IComparable
{
    //Data fields
    private int patientNumber;
    private string patientName;
    private int patientAge;
    private double patientAmountDue;

    //Constructors
    public Patient(): this(9,"ZZZ",0,0.00)
    {
    }
    public Patient(int _patientNumber, string _patientName, int _patientAge, double _patientAmountDue)
    {
       PatientNumber = _patientNumber;
       PatientName = _patientName;
       PatientAge = _patientAge;
       PatientAmountDue = _patientAmountDue;
    }
    //Properties
    public int PatientNumber
    { 
       get { return patientNumber; }
       set { patientNumber = value; }
    }
    public string PatientName
    {
       get { return patientName; }
       set { patientName = value; }
    }
    public int PatientAge
    {
       get { return patientAge; }
       set { patientAge = value; }
    }
    public double PatientAmountDue
    {
       get { return patientAmountDue; }
       set { patientAmountDue = value; }
    }

    //Interfaces
    int IComparable.CompareTo(Object o)
    {
       int returnVal; //temporary value container

       Patient temp = (Patient)o; //create temp instance of the class
       if (this.PatientNumber > temp.PatientNumber)
          returnVal = 1;
       else
          if (this.PatientNumber < temp.PatientNumber)
             returnVal = -1;
          else
             returnVal = 0; //exact match
       return returnVal;
    }
}

您可以将所有患者编号放入HashSet<int>并通过Contains()测试是否为一个编号分配了编号:

class Patient : IComparable { 
  ...
  // Simplest, not thread safe
  private static HashSet<int> s_AllocatedPatientNumbers = new HashSet<int>();

  public static Boolean IsNumberAllocated(int patientNumber) {
    return s_AllocatedPatientNumbers.Contains(patientNumber);
  } 

  public int PatientNumber { 
    get { 
      return patientNumber; 
    }
    set { 
      s_AllocatedPatientNumbers.Remove(patientNumber);
      patientNumber = value; 
      s_AllocatedPatientNumbers.Add(patientNumber);
    }
  }
}

因此,每当您需要测试是否已分配了数字时,就无需创建临时患者对arra y进行排序等。只需一个简单的调用即可:

  if (Patient.IsNumberAllocated(intInputValue)) {
    ...
  } 

用字典替换数组,建议与唯一键一起使用:

Dictionary<int,Patient> patients = new Dictionary<int,Patient>();

while (true)
{
    Console.Write("\nEnter Patient Account Number: ");
    int number = Convert.ToInt32(Console.ReadLine());

    if (patients.ContainsKey(number))
    {
        Console.Write("\nSorry, Patient Account Number {0} is a duplicate.", number);
        Console.Write("\nPlease re-enter the Patient Account Number: ");
        continue;
    }

    Console.Write("***Patient Account Number unique and accepted***\n");
    Console.Write("Enter the Patient Name: ");
    string name = Console.ReadLine();
    Console.Write("Enter the Patient Age: ");
    int age = Convert.ToInt32(Console.ReadLine());
    Console.Write("Enter the total annual Patient amount due: ");
    double amountDue = Convert.ToDouble(Console.ReadLine());
    patients.Add(number, new Patient(number, name, age, amountDue));
}

现在,该循环缺少退出条件。

编辑:虽然这不能回答标题的问题,但我认为这是OP所寻找的。

这个

Patient[] tempobjectPatient = new Patient[intMaxNum];

创建一个新数组并将其分配给tempobjectPatient 但是这个新数组从未使用过,因为这里

tempobjectPatient = objectPatient;

您立即将旧的分配给tempobjectPatient 因此,在此之后,您将没有两个数组实例。 tempobjectPatientobjectPatient引用相同的实例。

您可能想要:

Patient[] tempobjectPatient = (Patient[])objectPatient.Clone();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM