sinfella
sinfella

Reputation: 286

List<Model> Copies same data in list items in foreach loop

I am looping through a collection of XML nodes, i am trying to save the results to a list<> (There are two items in the XML) but on the second loop, Both list items are the same, so it is copying the last read data to both Lists.

//Test Node Details - If not passed
                if (record.PCBResult != "Passed")
                {
                   
                    TestResultsModel testRecord = new TestResultsModel();
                    record.TestResults = new List<TestResultsModel>();
                    foreach (XElement element in xmlDoc.Descendants("Test"))
                    {
                        testRecord.TestName = element.Attribute("Name").Value;
                        testRecord.TestType = element.Attribute("Type").Value;
                        testRecord.TestItemNumber = element.Attribute("Itemnumber").Value;
                        testRecord.TestSide = element.Attribute("Side").Value;
                        testRecord.TestResult = element.Attribute("Result").Value;
                        testRecord.TestFailure = element.Attribute("Failure").Value;
                        testRecord.TestAoiFailure = element.Attribute("AOIFailure").Value;
                        
                        record.TestResults.Add(testRecord);
                    }
                    
                }

I thought the record.TestResults.Add would add the collection to the list and then add the new collection to the second list, on the second iteration of the foreach loop.. What am i doing wrong? Sorry, im a newbie.

Upvotes: 0

Views: 387

Answers (1)

Bulle-dog
Bulle-dog

Reputation: 34

I think your testRecord variable scope is not okay. "record.TestResults.Add" not make a copy of your object but a reference, when you update your object it's update in the list, you must place it in the loop to create a new object for each iteration, try something like that :

if (record.PCBResult != "Passed")
            {
                record.TestResults = new List<TestResultsModel>();
                foreach (XElement element in xmlDoc.Descendants("Test"))
                {
                    TestResultsModel testRecord = new TestResultsModel();

                    testRecord.TestName = element.Attribute("Name").Value;
                    testRecord.TestType = element.Attribute("Type").Value;
                    testRecord.TestItemNumber = element.Attribute("Itemnumber").Value;
                    testRecord.TestSide = element.Attribute("Side").Value;
                    testRecord.TestResult = element.Attribute("Result").Value;
                    testRecord.TestFailure = element.Attribute("Failure").Value;
                    testRecord.TestAoiFailure = element.Attribute("AOIFailure").Value;
                    
                    record.TestResults.Add(testRecord);
                }
                
            }

See that post for more details : list.add seems to be adding a reference to the original object?

Upvotes: 1

Related Questions