2

I'm working on a school project and everything was going alright before I though about the patients management. I've to create a program in a form that is able to manage patients and store them in memory, I thought about using a struct but then I changed to class.

I've to save the patient id, name, age, address, e-mail and store the medical interventions (might be many so I thought about using another array for that) he had so I made the class Patient like this:

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

And by introducing a new patient I would use List with add like this:

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 });
}

Is this correct? And how can I use foreach search the array for the id that the user introduced to get all the remaining information? If I'm wrong, how should I be doing it since I want to store all that data in memory.

Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
André
  • 703
  • 1
  • 6
  • 18

3 Answers3

1

Is this correct?

Logically, this is how you add an patient to a List<Patient>. I think I would do a couple of things differently.

  1. Use properties instead of fields so in the future you could add validation if necessary (e.g, a validation that the Email property is actually a valid email), and make them follow C# naming conventions:

    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. I'm not sure what Intervencoes stands for, but I wouldn't make it an Array type. I'd actually use the type you want to use, for example a Paciente[].

  3. If you want to create an in-memory lookup of a patient by his/her id, I'd create a Dictionary<int, Paciente>, which has an O(1) lookup (given that the id is unique):

    var pacienteById = new Dictionary<int, Paciente>();
    pacienteById.Add(paciente.Id, paciente);
    
Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
  • Intervencoes stands for "Medical Interventions", I would choose the interventions later that could be stored into an array because they can be many such as: [Surgery Tooth X, Removal of Tooth Y, ...] – André Dec 06 '15 at 09:01
  • So they're an [`Enum`](https://msdn.microsoft.com/en-us/library/system.enum(v=vs.110).aspx)? – Yuval Itzchakov Dec 06 '15 at 09:04
  • Ideally, I would like to be able to write in a textBox later what was the patient medical interventation and all those interventions would be added to something like a ListBox. – André Dec 06 '15 at 09:12
  • Looks like a `string[]` then. – Yuval Itzchakov Dec 06 '15 at 09:32
  • But won't I store all those string into a list? – André Dec 06 '15 at 09:33
  • I'm not sure what you mean. I think you need a little help with your design and I'm not sure this is the right place for it. Perhaps [CodeReview](http://codereview.stackexchange.com) will be of more help. – Yuval Itzchakov Dec 06 '15 at 09:34
  • 1
    I'll look forward. Thank you for the help, Yuval. – André Dec 06 '15 at 09:36
0

Now whenever you want to add a patient, you create a new List of patients instead and append the new patient to it.
Is that a desired behavior?
Anyway, you can
foreach (Paciente patient in pacientes) to iterate through all the patients, provided you take the List<Paciente> declaration out of the function.
Then you may patient.id or patient.email to access the different fields, though I would recommend adding {get, set} methods to them.
Also, I would make all the fields in your Paciente class private.

Idos
  • 15,053
  • 14
  • 60
  • 75
  • Oh.. I forgot about that. I honestly just want to have an array (or any form of storing data) which will contain the patients (id, name, address, ..., and the medical interventions that will be stored in another list since they can be many). Is a list the best idea? And I want it to store the new patient whenever I fill the textboxes with the ID, Name, ... into the array/list whenever I click the button "Add Patient". – André Dec 06 '15 at 08:40
  • Yes a list is good. Your code is ok. Just take the array out of the function and instead do pacientes.add(pacient) whenever you click the button. Then you can fill all the fields inside the function. But I really recommend implementing getters and setters for the Paciente class. – Idos Dec 06 '15 at 08:42
  • What's getters and setters? I'm sorry, I'm just really new on C#, came from Python and I work on it for less than 2 weeks, it has been kinda confuse. So the array out of the function and the Pacientes.Add(...) inside? Using `foreach (Paciente patient : patients)` gives me the error: **'Paciente' is a type, which is not valid...** – André Dec 06 '15 at 08:54
  • `for (Paciente patient : patients)` is Java syntax. – Yuval Itzchakov Dec 06 '15 at 08:55
  • This explains getters and setters perfectly http://stackoverflow.com/questions/1568091/why-use-getters-and-setters – Idos Dec 06 '15 at 09:00
  • I got it, still, I'm having an issue when moving the list out of the button click control, it gives me an error on **pacientes**.Add(...);, it doesn't exist in it's context. – André Dec 06 '15 at 09:24
  • Ugh, I'm sorry for all the confusion and mess. I think I got it working, is it correct like this? [link](http://i.imgur.com/HYpdpYM.png) – André Dec 06 '15 at 09:31
  • Great! Thank you for the big help and for the patience, Idos. – André Dec 06 '15 at 09:37
0

You should try this:

   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();
Idos
  • 15,053
  • 14
  • 60
  • 75
jamir
  • 62
  • 1
  • 8