Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to have a form that send a List of my own object to an other form on the loading of the second form.
I tried to do like these 2 people shows, but it doesn't work.

https://stackoverflow.com/questions/2957388/passing-object-to-different-windows-forms

https://www.codeproject.com/Articles/14122/Passing-Data-Between-Forms


Here is the 2 ways to do what I tried to do:

public List<MyObject> MyObjects { get; set; }


And

private List<MyObject> MyObjects { get; set; }

public Form2(List<MyObject> MyObjectsReceived)
{
   InitializeComponent();
   MyObjects = MyObjectsReceived;
}


I get an error by doing that, but with something like strings, it just works.
Both errors are kind of the same, but the error for the first exemple I showed is this:

Inconsistent accessibility, List<myobject> is less accessible than the properties Form2.MyObjects

This object works kind of everywhere else and this line of code works.

private List<MyObject> MyObjects { get; set; }



I don't know why when something is public, nothing works. If you know an other way to send an object like that form a from to an other one, let me know.

Thanks for your help!

What I have tried:

public List<MyObject> MyObjects { get; set; }


and

private List<MyObject> MyObjects { get; set; }

public Form2(List<MyObject> MyObjectsReceived)
{
   InitializeComponent();
   MyObjects = MyObjectsReceived;
}
Posted
Updated 17-Feb-21 9:11am
v2

Your issue is not quite clear...

If you have declared List<MyObject> in Program.cs class as:
C#
public static List<MyObject> MyObjects;

than that list is accesible everywhere in your application. You don't need to pass it between forms.

If your List<MyObject> is declared in Form1 and its "visibility" is set to private, then you need to pass that list to Form2 to be able to use it. Further, if you would like to return that list via Form2 property, then that property have to be public.
Form2.cs
C#
private List<MyObject> mo;
//costructor
public Form2(List<MyObject> mos)
{
    mo = mos;
}

public List<MyObject> MyObjects
{
    get=> mo;
}


Form1.cs
C#
private List<MyObject> MyForm1Objects;
//method to call Form2
using (Form2 frm = new Form2(MyForm1Objects))
{
    DialogResult dr = frm.ShowDialog();
    if(dr= DialogResult.OK)
    {
        //use Form2 list
        MyForm1Objects = frm.MyObjects;
        //further instructions
    }
}


Conclusion: we need more details about your issue.
 
Share this answer
 
See this CodeProject article for a more in depth discussion:
Transferring information between two forms, Part 1: Parent to Child[^]
 
Share this answer
 
Well in my case, I just needed a public on my class MyObject:

public class MyObject{


}
 
Share this answer
 
Comments
RickZeeland 17-Feb-21 15:19pm    
Congrats, but what do you do when you want to send data back to the main form?
:)
Etienne-Perinet 17-Feb-21 16:50pm    
Well I did not need to do that, but if you want to share information between the 2 form, I gess you can just have the 2 forms sharing the same object that contains the informations needed.

On Form1:

public partial class Form1 : Form
    {
        MyObject mainObject = new MyObject();
        public Form1()
        {
            InitializeComponent();

            mainObject.Name = "I am a test!";

            Form2 form2 = new Form2(ref mainObject);
            form2.Show();

            MessageBox.Show(mainObject.Name);
        }
    }

and on Form2:
    public partial class Form2 : Form
    {
        MyObject mainObject;
        public Form2(ref MyObject myObject)
        {
            mainObject = myObject;
            InitializeComponent();

            mainObject.Name = "I am not a test";
        }
    }

The 2 objects will share the same reference so I gess you can use all your data in this object instance. I tried this code and it worked out really well.
Etienne-Perinet 17-Feb-21 16:55pm    
I showed "I am not a test"
RickZeeland 18-Feb-21 1:12am    
Looks ok to me!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900