gpt4 book ai didi

c# - 用于患者管理的列表/数组

转载 作者:太空宇宙 更新时间:2023-11-03 12:55:17 25 4
gpt4 key购买 nike

我正在做一个学校项目,在我考虑患者管理之前一切都很顺利。我必须以能够管理患者并将其存储在内存中的形式创建一个程序,我考虑过使用结构,但后来我改为使用类。

我必须保存他拥有的患者 ID、姓名、年龄、地址、电子邮件并存储医疗干预措施(可能很多,所以我考虑为此使用另一个数组)所以我将 Patient 类设置为这样:

public class Paciente
{
public int id;
public string nome;
public int idade;
public string morada;
public string contato;
public string email;
public Array intervencoes;
}

通过引入新患者,我会像这样使用 List 和 add:

private void bAdicionar_Click(object sender, EventArgs e)
{
List<Paciente> pacientes = new List<Paciente>();
pacientes.Add(new Paciente { id = Convert.ToInt32(txtID.Text), nome = txtNome.Text, idade = Convert.ToInt32(txtIdade.Text), morada = txtMorada.Text, contato = txtNumero.Text, email = txtEmail.Text });
}

这是正确的吗?我如何使用 foreach 在数组中搜索用户引入的 id 以获取所有剩余信息?如果我错了,我应该怎么做,因为我想将所有数据存储在内存中。

最佳答案

Is this correct?

从逻辑上讲,这就是您将患者添加到 List<Patient> 的方式.我想我会做一些不同的事情。

  1. 使用 properties而不是字段,因此将来您可以在必要时添加验证(例如,验证 Email 属性实际上是一封有效的电子邮件),并使它们遵循 C# 命名约定:

    public class Paciente
    {
    public int Id { get; set; }
    public string Nome { get; set; }
    public int Idade { get; set; }
    public string Morada { get; set; }
    public string Contato { get; set; }
    public string Email { get; set; }
    public Array Intervencoes { get; set; }
    }
  2. 我不确定是什么Intervencoes代表,但我不会把它变成 Array 类型。我实际上会使用您想要使用的类型,例如 Paciente[] .

  3. 如果您想通过他/她的 ID 在内存中查找患者,我会创建一个 Dictionary<int, Paciente> ,它有一个 O(1) 查找(假设 id 是唯一的):

    var pacienteById = new Dictionary<int, Paciente>();
    pacienteById.Add(paciente.Id, paciente);

关于c# - 用于患者管理的列表/数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34115366/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com