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

C# 用于患者管理的列表/阵列,c#,arrays,list,class,struct,C#,Arrays,List,Class,Struct,我正在做一个学校项目,在我考虑病人管理之前,一切都很顺利。我必须创建一个能够管理患者并将其存储在内存中的程序,我曾想过使用struct,但后来我改为class 我必须保存患者id、姓名、年龄、地址、电子邮件并存储医疗干预措施可能很多,因此我考虑使用另一个数组来存储他所拥有的信息,因此我将类patient设置为: public class Paciente { public int id; public string nome; public int idade;

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

我必须保存患者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;
}
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 });
}
通过介绍一位新患者,我将使用列表和add,如下所示:

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

现在,无论何时你想添加患者,你都要创建一个新的患者列表,并将新患者附加到其中。这是你想要的行为吗?无论如何,只要将列表声明从函数中移除,就可以让pacientes中的每个Paciente患者迭代所有患者。然后您可以使用patient.id或patient.email来访问不同的字段,不过我建议您向它们添加{get,set}方法。另外,我会让你的Paciente班级的所有领域都是私有的

您应该尝试以下方法:

   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();
这是正确的吗

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

使用而不是字段,以便将来您可以在必要时添加验证,例如,验证电子邮件属性实际上是有效电子邮件,并使其遵循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; }
}
我不确定Intervencoes代表什么,但我不会让它成为一种类型。实际上,我会使用您想要使用的类型,例如Paciente[]

如果要根据患者的id创建内存中的查找,我会创建一个,它具有O1查找,因为id是唯一的:

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

哦我忘了。老实说,我只想有一个数组或任何形式的存储数据,其中将包含患者id、姓名、地址……和医疗干预措施,这些数据将存储在另一个列表中,因为它们可能很多。列表是最好的主意吗?每当我在文本框中填入ID、姓名等时,我希望它存储新患者。。。每当我单击“添加患者”按钮时,都会将患者添加到数组/列表中。是的,列表很好。你的代码没问题。只需将数组从函数中取出,并在单击按钮时执行pacientes.addpacient即可。然后可以填充函数中的所有字段。但是我真的建议为Paciente类实现getter和setter。什么是getter和setter?很抱歉,我对C非常陌生,来自Python,我在C上工作了不到两周,有点困惑。因此,函数外的数组和Pacientes.Add。。。在…内使用foreach Paciente patient:patients给了我一个错误:“Paciente”是一个类型,它无效…对于Paciente patient:patients是Java语法。这解释了Getter和Setter完美地干涉了Coes代表医疗干预,我会选择稍后可以存储到阵列中的干预措施,因为它们可以是很多,比如:[手术牙齿X,移除牙齿Y,…]所以它们是一个理想的,我希望以后能够在文本框中写下患者医疗干预的内容,所有这些干预都将添加到类似列表框的内容中。然后看起来像字符串[]。我将期待。谢谢你的帮助,尤瓦尔。