用于患者管理的列表/阵列



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

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

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

通过介绍一个新的病人,我会使用列表添加如下:

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以获得所有剩余信息?如果我错了,我应该怎么做,因为我想把所有的数据都存储在内存中。

这是正确的吗?

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

  1. 使用属性而不是字段,以便将来在必要时添加验证(例如,验证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);
    

现在,每当您想添加患者时,都会创建一个新的患者List,并将新患者附加到其中。
这是所需的行为吗
无论如何,只要从函数中取出List<Paciente>声明,就可以
foreach (Paciente patient in pacientes)遍历所有患者
然后您可以使用patient.idpatient.email来访问不同的字段,不过我建议为它们添加{get, set}方法
此外,我会将您的Paciente类中的所有字段设置为private

你应该试试这个:

   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<Paciente> pacientes = new List<Paciente>();
         pacientes.Add(new Paciente { id = 1, nome = "amir", idade = 121, morada = "moradatest1", contato = "contatotest1", email = "emailtext1" });
         pacientes.Add(new Paciente { id = 2, nome = "amir2", idade = 123, morada = "moradatest2", contato = "contatotest2", email = "emailtext2" });
         pacientes.Add(new Paciente { id = 3, nome = "amir3", idade = 123, morada = "moradatest3", contato = "contatotest3", email = "emailtext3" });

         IEnumerable<Paciente> selectedPatient = from pac in pacientes
                                    where pac.id == 1
                                    select pac;
         foreach (var item in selectedPatient)
         {
             Console.WriteLine("{0},{1},{2},{3},{4}", item.id, item.nome, item.idade, item.morada, item.contato,item.email);
         }
         Console.ReadLine();

最新更新