IList.Add() overwriting existing data [duplicate]

柔情痞子 提交于 2019-12-10 14:58:30

问题


I'm facing a problem adding data to an IList but the problem is each time I added data the existing data is overwritten with the current one my code is given below:

Test test = new Test();
IList<Test> myList = new List<Test>();

foreach (DataRow dataRow in dataTable.Rows)
{
     test.PatientID = Convert.ToInt64(dataRow.ItemArray[0]);
     test.LastName = dataRow.ItemArray[1].ToString();
     test.FirstName = dataRow.ItemArray[2].ToString();
     myList.Add(test);
}

What's the reason behind this?


回答1:


move test object creation inside the loop

IList<Test> myList = new List<Test>();

foreach (DataRow dataRow in dataTable.Rows)
{   Test test =new Test();
    test.PatientID = Convert.ToInt64(dataRow.ItemArray[0]);
    test.LastName = dataRow.ItemArray[1].ToString();
    test.FirstName = dataRow.ItemArray[2].ToString();
    myList.Add(test);
 }

what you currently doing is updating same instant of test inside the loop and add the same again and again..




回答2:


You need to move the the creation of Test object to inside the loop. The reason is that the object new Test() is instantiated only once and the reference to the same object keeps getting added to the list in the loop.




回答3:


Because Test test is being copied by reference. You need to move it inside the loop.

do it like this

IList<Test> myList = new List<Test>();
foreach (DataRow dataRow in dataTable.Rows)
{
     Test test =new Test();
     test.PatientID = Convert.ToInt64(dataRow.ItemArray[0]);
     test.LastName = dataRow.ItemArray[1].ToString();
     test.FirstName = dataRow.ItemArray[2].ToString();
     myList.Add(test);
}



回答4:


Because you don't create new instance in foreach loop, correct the code:

Test test = null;
IList<Test> myList = new List<Test>();

foreach (DataRow dataRow in dataTable.Rows)
{
   test = new Test();
   test.PatientID = Convert.ToInt64(dataRow.ItemArray[0]);
   test.LastName = dataRow.ItemArray[1].ToString();
   test.FirstName = dataRow.ItemArray[2].ToString();
   myList.Add(test);
}



回答5:


You're using the same reference variable (test) all the time. Try creting new Test() in your loop.




回答6:


You are creating a single instance of Test

Test test = new Test(); // This is your instance
IList<Test> myList = new List<Test>();

foreach (DataRow dataRow in dataTable.Rows)
{
    // Here you change the values of the existing instance each time you loop
    test.PatientID = Convert.ToInt64(dataRow.ItemArray[0]);
    test.LastName = dataRow.ItemArray[1].ToString();
    test.FirstName = dataRow.ItemArray[2].ToString();
    myList.Add(test); // but you are still just adding the same reference to the list multiple times
}

And then since you are never creating a new Test instance, you are adding the same reference to the list multiple times. This means that you are essentially just storing the same object over and over: if you make any changes to one item in the list it will immediately be visible in all others because they are essentially the same object

The solution is to move the instantiation of test inside the loop

IList<Test> myList = new List<Test>();

foreach (DataRow dataRow in dataTable.Rows)
{
    Test test = new Test(); // Each loop iteration will now create a new instance of Test
    test.PatientID = Convert.ToInt64(dataRow.ItemArray[0]);
    test.LastName = dataRow.ItemArray[1].ToString();
    test.FirstName = dataRow.ItemArray[2].ToString();
    myList.Add(test);
}

If you need to understand this better, look at reference and value types in .NET and passing by reference/value

Value and Ref types in .NET: http://msdn.microsoft.com/en-us/library/t63sy5hs.aspx

Some info on pointers on Wikipedia http://en.wikipedia.org/wiki/Pointer_(computer_programming)



来源:https://stackoverflow.com/questions/19156612/ilist-add-overwriting-existing-data

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!