c# need help with adding an object to a list of objects

Dataporter 21 Reputation points
2021-01-02T19:55:49.077+00:00

namespace Pread835
{
public class LineItem
{
public string ClaimNo { get; set; }
public string PatID { get; set; }
public string ProcCode { get; set; }
public string ChargeAmt { get; set; }
public string PaidAmt { get; set; }
public string Dos { get; set; }
public string COadj { get; set; }
public string PR1adj { get; set; }
public string PR2adj { get; set; }
public string PR3adj { get; set; }
public string TranNo { get; set; }

    public LineItem Initialize()
    {
        this.ClaimNo = "";
        this.ChargeAmt = "";
        this.COadj = "";
        this.Dos = "";
        this.PatID = "";
        this.ProcCode = "";
        this.PaidAmt = "";
        this.PR1adj = "";
        this.PR2adj = "";
        this.PR3adj = "";
        this.TranNo = "";
        return this;

    }

}}

public List<LineItem> lineList = new List<LineItem>();

LineItem t = new LineItem();

t.Initialize();

// the beginning of a loop where I am reading through a file to get values

 t.ClaimNo = c.ClaimNo;
 t.ProcCode = theSeg[1].Substring(3);
 t.ChargeAmt = theSeg[2];
 t.PaidAmt = theSeg[3];

 lineList.Add(t);
 t.Initialize();

// end of the loop

The values of t do change in the loop

However only the values in the first t are written over and over to the list..

What am I doing wrong?

Thanks in advance for your help
Dave

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,011 questions
0 comments No comments
{count} votes

Accepted answer
  1. Alberto Poblacion 1,561 Reputation points
    2021-01-02T20:48:01.71+00:00

    Make sure that the line
    LineItem t = new LineItem();
    is placed inside the loop, not before entering the loop.

    The reason is that when you do
    lineList.Add(t);
    it adds a reference to t into the list, not a copy of t.

    Therefore, every time that you change the value of t, it changes all the elements inside the list, since they are all referring to the (only) copy of t that exists in memory and that you created with the original "new" outside the loop. As mentioned before, the remedy is to do the "new" on every iteration of the loop.


3 additional answers

Sort by: Most helpful
  1. Viorel 118K Reputation points
    2021-01-02T20:16:47.793+00:00

    Since it is a class, try putting

    LineItem t = new LineItem();
    t.Initialize();
    

    inside the loop. Also remove the last t.Initialize().


  2. Dataporter 21 Reputation points
    2021-01-02T20:57:44.457+00:00

    Okay, apparently the values of the LAST t are written over and over to the list.

    Or, apparently when I change t I am changing every element in the list.

    The values in the list should be unique. I still don't understand what is wrong or how to fix it.


  3. Dataporter 21 Reputation points
    2021-01-02T21:26:41.543+00:00

    Apoblacion has the correct answer! Thank you!

    Also FYI this is the "improved" way to do it

                       LineItem t = new LineItem
                        {
                            ClaimNo = c.ClaimNo,
                            ProcCode = theSeg[1].Substring(3),
                            ChargeAmt = theSeg[2],
                            PaidAmt = theSeg[3],
                            Dos = theDos,
                            COadj = theCOadj
    
                        };
                        lineList.Add(t);
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.