Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, every time i add an another List to List the first data is being tampered.
all i want is to add a new set of data without affecting the previous data inside of my List. Kindly check my code below i know I'm not a good explainer.

baplieContainerDO oBaplieContainerDO;
       List<List<baplieContainerDO>> setBaplieContainerDetails;
       public Form1()
       {
           InitializeComponent();
           oBaplieContainerDO = new baplieContainerDO();
           setBaplieContainerDetails = new List<List<baplieContainerDO>>();
       }

       private void button1_Click(object sender, EventArgs e)
       {
           List<List<baplieContainerDO>> AA = getBaplieContainerDetails();
           foreach (var List in AA)
           {
               foreach (var item in List)
               {
                   string a = item.stowageCell;
                   string b = item.freeText;
                   string c = item.containerNumber;
               }
           }
       }
       public List<List<baplieContainerDO>> getBaplieContainerDetails()
       {
           var arlist1 = new ArrayList();
           for (int i = 0; i < 10; i++)
           {
               oBaplieContainerDO.stowageCell = i.ToString();
               oBaplieContainerDO.freeText = i.ToString();
               oBaplieContainerDO.containerNumber = i.ToString();
               //arlist1.Add(oBaplieContainerDO);
               setBaplieContainerDetails.Add(new List<baplieContainerDO> { oBaplieContainerDO });
           }
           return setBaplieContainerDetails;
       }
       public class baplieContainerDO
       {
           public string stowageCell { get; set; }
           public string freeText { get; set; }
           public string containerNumber { get; set; }
       }


What I have tried:

i tried array list but it's the same
Posted
Updated 14-Apr-21 22:40pm
Comments
Anurag Gandhi 15-Apr-21 4:04am    
myList.AddRange(anotherList);

Provided both the lists are of same List<t> type.
Reden Rodriguez 15-Apr-21 4:18am    
you want me to change my List<bapliecontainerdo> to List<string>? if yes i already tried this and yes this will work but i need my Class inside of List..

1 solution

I don't understand why you're using a List<List<baplieContainerDO>>, doesn't quite make sense since you're only ever adding one item to that nested list. But according to your question:
first data is being tampered

This is because you create your oBaplieContainerDO object only once and then you store it against the class in a field. Then later when in your loop you're updating the same object instance over and over:
C#
oBaplieContainerDO.stowageCell = i.ToString();
oBaplieContainerDO.freeText = i.ToString();
oBaplieContainerDO.containerNumber = i.ToString();

You need to create a new instance of this class in each loop instead of updating the existing object, that's why all of the elements in the list are being updated to the same values.
 
Share this answer
 

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