NooooobDeveloper
NooooobDeveloper

Reputation: 71

Fill a list of objects from another class

I know this is a basic question, but I am struggling with it for quite some time now..

I have a main form in which I have a List<Filenames> and I have another class that gets invoked by the main form. This class has to insert some entries in my List<Filenames>.

In main form, I have the list initialized as:

public List<Filenames> filenamesList = new List<Filenames>();

And in the other class I do:

MainForm mainForm = new MainForm();
mainForm.filenamesList.Clear();

//then I have a for loop in which I perform some changes

mainForm.filenamesList.Add(new Filenames { name = "filename1", id = 1 } );
//it works until here and a new Filenames is added to the list
//then we go back to the main form

The problem is that now the List<Filenames> is empty. What am I doing wrong?

Upvotes: 0

Views: 1391

Answers (3)

MeanGreen
MeanGreen

Reputation: 3305

By declaring a new MainForm using the line MainForm mainForm = new MainForm(); you are not referring to the same filenamesList. You need to get the reference to the first class, either by sending the filenamesList as a parameter to the function or by adding it as a propery in one of the classes.

If the "other class" is a child of the main form, you might be able to get a reference to the parent object, but this depends on the stucture of the program.

Upvotes: 0

Grant Winney
Grant Winney

Reputation: 66439

When you do this in the second form:

MainForm mainForm = new MainForm();

You've created an entirely new instance of MainForm that has no reference to the list in the original MainForm instance. You're clearing the list and adding items to it, but when you exit your second form those items are lost.

You've got to pass your original MainForm instance to the new form that opens:

public class Form2 : Form
{
    private MainForm mainForm;

    public Form2(MainForm mainForm)
    {
        this.mainForm = mainForm;
    }

    public void SomeMethodThatUpdatesFileNamesList()
    {
        mainForm.filenamesList.Clear();
        ...
    }
}

Upvotes: 0

D Stanley
D Stanley

Reputation: 152501

I have another class that gets invoked by the main form

I suspect the problem is here:

MainForm mainForm = new MainForm();

This creates a new MainForm instance, which is not the same instance as the MainForm that invokes the function.

I would redesign this so that the function you are invoking returns a List<Filenames> (or just an IEnumerable<Filenames> to be less specific) and let the form class worry about filling its filenamesList property. Exposing data members of a form can get messy quickly since you need to pass an instance of the form to anything that must use the property.

soimething like this:

-- other class --

public IEnumerable<Filenames> GetFilenames()
{
    return new List<Filenames> { new Filenames { name = "filename1", id = 1 } };
}

--MainForm--

this.filenamesList.Clear();
this.filenamesList.AddRange(otherclass.GetFilenames());

Upvotes: 1

Related Questions