Anais
Anais

Reputation: 627

Passing List of objects to another form c#

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

Upvotes: 0

Views: 3068

Answers (4)

You can do the following

Sender Form:

private void btnIniciar_Click(object sender, EventArgs e)
    {
        this.Hide();
        Main newMain = new Main(this.lsProcesos);
        newMain.ShowDialog();
        this.Show();

    }

Receiver Form:

public partial class Main : Form
{
    List<Proceso> lsProcesos;
    public Main(object sender)
    {
        InitializeComponent();
        lsProcesos = (List<Proceso>)sender;
    }
}

Upvotes: 0

ZippyZippedUp
ZippyZippedUp

Reputation: 458

Move the instantiation of Form1 and listaPacienti into the constructor. This existing answer provides a good description of why you are getting this error - A field initializer cannot reference the nonstatic field, method, or property.

public partial class Form2 : Form
    {

        List<Pacient>listaPacienti;
        Pacient p1 = new Pacient(0, "Pacient1", 0, 200);
        Pacient p2 = new Pacient(1, "Pacienct2", 0, 100);

        Form1 formMedici;

        public Form2()
        {
            listaPacienti=new List<Pacient>();
            formMedici = new Form1(listaPacienti);

            InitializeComponent();
            listaPacienti.Add(p1);
            listaPacienti.Add(p2);
        }
}

Upvotes: 0

David
David

Reputation: 218798

There are several things wrong here actually. The error itself is telling you that you can't use a class member when initializing a class member. You're trying to use listaPacienti to initialize formMedici. But you could initialize it in the constructor. Something like this:

List<Pacient> listaPacienti = new List<Pacient>();
Pacient p1 = new Pacient(0, "Pacient1", 0, 200);
Pacient p2 = new Pacient(1, "Pacienct2", 0, 100);

Form1 formMedici;

public Form2()
{
    InitializeComponent();
    listaPacienti.Add(p1);
    listaPacienti.Add(p2);
    formMedici = new Form1(listaPacienti);
}

However, your Form1 constructor also needs to accept that parameter. So you either need to modify the existing Form1 constructor or add a new one:

public Form1(List<Pacient> listaPacienti)
{
    InitializeComponent();
    // do something with listaPacienti?
}

However, it's not really clear why you're even doing this. You create an instance of Form2 in Form1:

Form2 formPacienti = new Form2();

Then you create an instance of Form1 in Form2:

Form1 formMedici = new Form1(listaPacienti);

This is an infinite recursion waiting to happen. Do you really want Form2 to create a new instance of Form1? Or do you want to use the existing instance of Form1? If the latter, then remove the references to Form1 entirely from Form2.

There are a variety of ways to accomplish the overall goal of what you're trying to do, and which one is ideal depends on more than what's presented here. But the simplest approach given the information present is probably to just make listaPacienti accessible from outside of Form2:

public List<Pacient> ListaPacienti { get; set; }
Pacient p1 = new Pacient(0, "Pacient1", 0, 200);
Pacient p2 = new Pacient(1, "Pacienct2", 0, 100);

public Form2()
{
    InitializeComponent();
    ListaPacienti = new List<Pacient>()
    ListaPacienti.Add(p1);
    ListaPacienti.Add(p2);
}

Now that listaPacienti is a public property, any code which has a reference to an instance of Form2 can access that property:

Form2 formPacienti = new Form2();
// Do other things with formPacienti?
// Either way, here you can access formPacienti.ListaPacienti

Upvotes: 4

mandeepsinghn
mandeepsinghn

Reputation: 94

You need to add one more constructor to form one like this.

public Form1(List<Pacient> list)
    {
        this.listaPacienti=list;
        InitializeComponent();

    }

After that you can go with this code as you are going.

Form1 formMedici = new Form1(listaPacienti);

Upvotes: 0

Related Questions