-1

The following code seems to add a new record to the list but overwrites all the record with the last record created. I can get it to work fine with ...

lpr.Add(new personRecord(){Age = pr.Age,Name = pr.Name});

but this seems more long winded and in the real app the record is much bigger.

private void button1_Click(object sender, EventArgs e)
{
    personRecord pr = new personRecord();

    List<personRecord> lpr = new List<personRecord>();

    pr.Age = 40;
    pr.Name = "Bob";
    lpr.Add(pr);

    pr.Age = 30;
    pr.Name = "Steve";
    lpr.Add(pr);

    pr.Age = 44;
    pr.Name = "Phil";
    lpr.Add(pr);

    pr.Age = 33;
    pr.Name = "Sue";
    lpr.Add(pr);
}

public class personRecord
{
    private int age;
    private string name;

    public int Age
    {
        get { return age; }
        set { age = value; }
    }

    public string Name
    {
        get { return name; }
        set { name = value; }
    }
}
Carlos G.
  • 4,564
  • 4
  • 34
  • 57
user3755946
  • 804
  • 1
  • 10
  • 22
  • 1
    what's the question? – user1666620 Oct 07 '15 at 14:40
  • I don't think you can get around it being long winded if you're manually inserting a bunch of records defined in code. What's the purpose of this? To seed a database with sample data? And what's your question? How do you make the code shorter? [Code Review](http://codereview.stackexchange.com/help/on-topic) might be a good place to ask this. – mason Oct 07 '15 at 14:42

3 Answers3

3

pr is a reference to an object. When you change the values of pr you are changing the values of the same object and adding that reference to the list. So your list consists of several references to the same object, and the last values you set will be reflected by each reference.

You can solve it by adding

pr = new personRecord();

before each block to ensure that pr is referenceing a new object each time.

When you do

lpr.Add(new personRecord(){Age = pr.Age,Name = pr.Name});

You are adding a reference to a new object and just copying the values from the single pr reference.

D Stanley
  • 149,601
  • 11
  • 178
  • 240
  • Or the object initializer syntax might be appropriate, on the `lpr` object. – mason Oct 07 '15 at 14:45
  • @mason possibly. but the main disconnect is the fact that the same instance is being modified - that's a more critical part of the problem than the syntax used. – D Stanley Oct 07 '15 at 14:46
  • Yes, most important to point out the flaw, but handy also to know what the options are for fixing it. – mason Oct 07 '15 at 14:46
2

Is this what you are trying to achieve?

private void button1_Click(object sender, EventArgs e)
{
  List<personRecord> lpr = new List<personRecord>
  {
    new personRecord { Age = 40, Name = "Bob" },
    new personRecord { Age = 30, Name = "Steve"},
    new personRecord { Age = 44, Name = "Phil"},
    new personRecord { Age = 33, Name = "Sue"},
 };
}
Cetin Basoz
  • 22,495
  • 3
  • 31
  • 39
0

Possible duplicate of what is different between Passing by value and Passing by reference using C#.

But as pr is a class this means that when you are adding it to the list you are actually only storing the reference to that variable. When you change that variable the next 3 times what you are actually changing is the values located at the memory of the original reference. So your list actually contains 4 references to the same object with the same values.

If you are attempting to reuse the same object (as this may be a model for your view) you should make a new object with it's values and add this new object to your list.

Community
  • 1
  • 1
Stephen Ross
  • 882
  • 6
  • 21