I have Form1 where I want to use the list of objects listaPacienti I created in Form2 but it doesn't work. Pacient is the class used to generate objects.
Here is Form1's beginning:
public partial class Form1 : Form
{
Form2 formPacienti = new Form2();
List<Pacient> listaPacienti = new List<Pacient>();
public Form1()
{
InitializeComponent();
}
And here is the beginning of Form2 where I created the list listaPacienti I want to pass to Form1:
public partial class Form2 : Form
{
List<Pacient>listaPacienti=new List<Pacient>();
Pacient p1 = new Pacient(0, "Pacient1", 0, 200);
Pacient p2 = new Pacient(1, "Pacienct2", 0, 100);
Form1 formMedici = new Form1(listaPacienti);
public Form2()
{
InitializeComponent();
listaPacienti.Add(p1);
listaPacienti.Add(p2);
}
What's wrong because I searched here before and that's how it is recommended to be done and if I wrote something wrong I don't know what it is because in Form2, the parameter "listaPacienti" from the following line
Form1 formMedici = new Form1(listaPacienti);
shows me this message: "A field initializer cannot reference the non-static field, method or property "Form2.listaPacienti".
Thank you