0

I am getting a problem with my list getting overwritten when trying to update it. I did find quite many references about this, but still can't apply them to my case.

Most of them suggest moving the declaration of variable inside a loop, but I am not using any loops at this stage.

There is a class based on the interface named Persons. I want to create a new Person when clicking the "add" button, the name of person is taken from the textbox.

The interface and the class for it:

interface IPerson
{
    //int getId();
    String GetFirstName();
    //String getLastName();
    //DateTime getDateOfBirth();
    //int getHeight();
}
 

public class Person: IPerson
{
    //public double MaximumValue;

    public string GetFirstName()
    {
        return Form1.newName;
    }
}

Button click:

    private void btnAdd_Click(object sender, EventArgs e)
    {
        newName = txtName.Text;

        //Person newPerson = new Person();
        //newPerson.GetFirstName();

        PersonCollection.ThisPersonCollection.Add();
    }

Form Load:

    private void Form1_Load(object sender, EventArgs e)
    {
        PersonCollection.ThisPersonCollection = new PersonCollection();

        PersonCollection.ThisPersonCollection.Create();
    }

Person collection class:

    class PersonCollection
{
    public static PersonCollection ThisPersonCollection;
    private static object _lock = new object();

    public static List<Person> _persons;
    public static List<Subscriber> _subscribers;

    private static System.Timers.Timer _timer = new Timer();
}

Add method in PersonCollection class

    public void Add()
    {
        lock (_lock)
        {
            Person newPerson = new Person();
            newPerson.GetFirstName();
            _persons.Add(newPerson);
        }
    }

I tried playing with static\non-static for all variables and classes, I tried to create a

newPerson = new Person();

in button click method and in Add method from the class, I tried to first add the new Person and only then alter it's values, I tried to create a Person as a method:

    public Person NewPerson()
    {
        Person newPerson = new Person();
        newPerson.GetFirstName();

        return newPerson;
    }

But neither of this did anything. From what I read, I do understand that when trying to add a new Person, instead I am overwriting all existing ones, because all list objects use same reference. But I can't get how to fix this.

Please help,

Evgenie

EvgenieT
  • 209
  • 1
  • 4
  • 16
  • maybe you creating a new List anyway every time you click the button. – Sabir Hossain Feb 20 '21 at 14:28
  • Adding entries to the `_persons` list is, as such, not doing anything else. You have to tell the listbox that it should update (or use data binding, but that's another topic). Also, you do not really need a `PersonCollection`. `List` is exactly that: A list of persons. – PMF Feb 20 '21 at 14:28
  • Can you please explain what you want? To get person name from a textbox, create person object, add it to collection and update dropdown list in the window? – Ivan Khorin Feb 20 '21 at 15:24
  • In case you run this code in a worker thread (based on `System.Timers.Timer` component and the `lock` statement), then the `List` is not a **thread-safe** list. See [this](https://stackoverflow.com/q/5874317/14171304) post for more. Don't skip the comments there. – dr.null Feb 20 '21 at 22:06

1 Answers1

0

Person class:

public interface IPerson
{
    public string FirstName { get; set; }
}

public class Person : IPerson
{
    public string FirstName { get; set; }
}

Form binding:

public BindingList<Person> Persons { get; private set; } = new();

public Form1()
{
    InitializeComponent();

    listBox1.DataSource = Persons;
    listBox1.DisplayMember = "FirstName";
}

private void button1_Click(object sender, EventArgs e)
{
    Persons.Add(new Person { FirstName = textBox1.Text });
}

private void button2_Click(object sender, EventArgs e)
{
    if (Persons.Count > 0)
        Persons.RemoveAt(Persons.Count - 1);
}

And don't stuck your brain.

Ivan Khorin
  • 827
  • 1
  • 5
  • 17