【问题标题】:When attempting to add a new item to a list I am erasing the list instead of creating a new object to add to the list尝试将新项目添加到列表时,我正在擦除列表而不是创建要添加到列表的新对象
【发布时间】:2017-01-19 21:34:36
【问题描述】:

目前我有两个按钮。创建一个 Patient 对象并向其添加费用详细信息,并将继续为每次单击添加这些详细信息的其他副本,然后将这些副本添加到 Patientlist。

第二个按钮应该创建 Patient 对象的一个​​新实例,以便它也可以执行上述相同的过程。

问题在于,当按下第二个按钮时,它会创建 Patient 对象的新实例,但列表会丢失之前的患者对象。

我觉得我在这里遗漏了一些非常明显的东西,并且已经回顾了复数视频、youtube 教程以及关于 SO 本身的很多问题。

static List<Patient> PatientsList()
    {
        List<Patient> patientList = new List<Patient>();
        return patientList;
    }

 private void AddAddOn_Click(object sender, EventArgs e)
    {
        // Set the data on the patient, name etc.
        patient.PatientFirstName = PatientFirstNameInput.Text;
        patient.PatientLastName = PatientLastNameInput.Text;
        patient.PatientCopay = Convert.ToDecimal(PatientCopayInput.Text);
        patient.BillId = BillIdInput.Text;

        Charge charge;

        // Does the patient already have a Charge in their list?
        if (patient.ChargeList.Count == 0)
        {
            // - if not, add a new charge
            charge = new Charge();
            patient.ChargeList.Add(charge);
        }
        else
        {
            // - if that's the case, use the existing charge but update info
            charge = patient.ChargeList.First();
        }


        // Add a charge to the patient's list of charges
        charge.DateofService = DateofServiceInput.Value.ToString("yyyyMMdd");
        charge.PrimaryProcedureCode = PrimaryProcedureInput.Text;
        charge.PrimaryChargeCost = Convert.ToDecimal(PrimaryChargeInput.Text);
        charge.PrimaryChargeContractualAdjustment = Convert.ToDecimal(PrimaryAdjustmentInput.Text);
        charge.PrimaryPaymentAmount = Convert.ToDecimal(PrimaryPaidInput.Text);

        // Set the name of the Patient
        // Create a new Charge
        // Create an add-on charge and add it to the Charge

        AddonCharge newAddonCharge = new AddonCharge();
        newAddonCharge.AddonProcedureCode = AddonProcedureInput.Text;
        newAddonCharge.AddonChargeCost = Convert.ToDecimal(AddonChargeInput.Text);
        newAddonCharge.AddonContractualAdjustment = Convert.ToDecimal(AddonAdjustmentInput.Text);
        newAddonCharge.AddonPaymentAmount = Convert.ToDecimal(AddonPaidInput.Text);
        charge.AddonChargeList.Add(newAddonCharge);

        List<Patient> patientList = PatientsList();
        patientList.Add(patient);
        //newCharge.AddonChargeList.Add(newAddonCharge);
    }

 private void AddtoListButton_Click(object sender, EventArgs e)
    {
        //return a new patient with null details
        Patient patient = new Patient();

        List<Patient> patientlist = PatientsList();
        // Show a messagebox with the string
        //MessageBox.Show(EDIToString());

    }

【问题讨论】:

  • PatientsList() 每次都会创建一个新列表。

标签: c# winforms list static-methods


【解决方案1】:

正如前面的答案所暗示的,您的问题是您在每次调用时都重新创建了患者列表。 static 关键字不会改变这一点。

我已更改您的代码来解决问题,方法是初始化患者列表字段一次。

private List<Patient> PatientsList = new List<Patient>();

private void AddAddOn_Click(object sender, EventArgs e)
{
    // Set the data on the patient, name etc.
    patient.PatientFirstName = PatientFirstNameInput.Text;
    patient.PatientLastName = PatientLastNameInput.Text;
    patient.PatientCopay = Convert.ToDecimal(PatientCopayInput.Text);
    patient.BillId = BillIdInput.Text;

    Charge charge;

    // Does the patient already have a Charge in their list?
    if (patient.ChargeList.Count == 0)
    {
        // - if not, add a new charge
        charge = new Charge();
        patient.ChargeList.Add(charge);
    }
    else
    {
        // - if that's the case, use the existing charge but update info
        charge = patient.ChargeList.First();
    }


    // Add a charge to the patient's list of charges
    charge.DateofService = DateofServiceInput.Value.ToString("yyyyMMdd");
    charge.PrimaryProcedureCode = PrimaryProcedureInput.Text;
    charge.PrimaryChargeCost = Convert.ToDecimal(PrimaryChargeInput.Text);
    charge.PrimaryChargeContractualAdjustment = Convert.ToDecimal(PrimaryAdjustmentInput.Text);
    charge.PrimaryPaymentAmount = Convert.ToDecimal(PrimaryPaidInput.Text);

    // Set the name of the Patient
    // Create a new Charge
    // Create an add-on charge and add it to the Charge

    AddonCharge newAddonCharge = new AddonCharge();
    newAddonCharge.AddonProcedureCode = AddonProcedureInput.Text;
    newAddonCharge.AddonChargeCost = Convert.ToDecimal(AddonChargeInput.Text);
    newAddonCharge.AddonContractualAdjustment = Convert.ToDecimal(AddonAdjustmentInput.Text);
    newAddonCharge.AddonPaymentAmount = Convert.ToDecimal(AddonPaidInput.Text);
    charge.AddonChargeList.Add(newAddonCharge);

    List<Patient> patientList = PatientsList;
    patientList.Add(patient);
    //newCharge.AddonChargeList.Add(newAddonCharge);
}

private void AddtoListButton_Click(object sender, EventArgs e)
{
    //return a new patient with null details
    Patient patient = new Patient();

    List<Patient> patientlist = PatientsList;
    // Show a messagebox with the string
    //MessageBox.Show(EDIToString());

}

【讨论】:

    【解决方案2】:

    你的问题从这个静态方法开始:

    static List<Patient> PatientsList()
    {
        List<Patient> patientList = new List<Patient>();
        return patientList;
    }
    

    每次调用时,都会创建一个全新的患者空列表。

    所以下面几行:

        List<Patient> patientList = PatientsList();
        patientList.Add(patient);
    

    将创建一个全新的空列表,然后将患者添加到其中。下次调用时,同样的事情会再次发生,之前的病人不再被存储。

    解决问题的最佳方法是将PatientsList() 方法转换为字段,如下所示:

    private readonly List<Patient> patientList = new List<Patient>();
    

    作为一个字段,patientList 字段会在构造类时自动初始化。

    readonly 关键字是可选的,但它基本上可以防止该字段被重新分配给其他内容。

    来自 MSDN:

    readonly 关键字是可以在字段上使用的修饰符。当一个 字段声明包括一个只读修饰符,分配给 声明引入的字段只能作为 声明或在同一类的构造函数中。

    现在,当您想将患者添加到列表中时,您只需从您的按钮单击方法中调用 patientList.Add(patient);

    【讨论】:

      【解决方案3】:

      您有一个用于创建患者列表的静态方法,感觉就像您期望它只被调用一次。不是这种情况。静态只是意味着它可以在没有任何类实例的情况下访问。

      您正在寻找的是将列表作为您班级的成员,并且只有在之前没有创建过的情况下才创建它。

      尝试在你的方法之外添加

      List<Patient> patientList = new List<Patient>();
      

      并删除您对 PatientList() 的调用;

      【讨论】:

        猜你喜欢
        • 2020-09-04
        • 2015-11-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多