1

When I call this method is always change the values in original List "printRowList", I don not want to change the original values. I just need to change the values of temp List which is "tempRowModellist". What can I do?

private List<PrintRowModel> SetTemplateSettingsData(
    List<PrintRowModel> printRowList, 
    object value)
{
    List<PrintRowModel> tempRowModellist = new List<PrintRowModel>();
    tempRowModellist.AddRange(printRowList);

    foreach (PrintRowModel printRow in tempRowModellist )
    {
        foreach (PrintColumnModel printColumn in printRow)
        {
            printColumn.Value = 
                GetObjectValues(printColumn.Value, value).ToString();
        }
    }

    return newList;
}
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Gayashan
  • 351
  • 1
  • 4
  • 13

4 Answers4

2

Because you are still referencing the original list. You need to clone it if you don't want to modify it. change this

tempRowModellist.AddRange(printRowList);`

as

tempRowModellist = printRowList.Clone().ToList();

add this extension method

static class Extensions
{
    public static List<T> Clone<T>(this List<T> listToClone) where T: ICloneable
    {
        return listToClone.Select(item => (T)item.Clone()).ToList();
    }
}

Note: Make sure that your class implements ICloneable interface.

Community
  • 1
  • 1
Ehsan
  • 31,833
  • 6
  • 56
  • 65
  • I got a error in "i=>i.Clone()).ToList();" I added the extension method but it say's The type "bla blaa" cannot be used as type parameter 'T' in the generic type or method "blaa blaa" There is no implicit reference conversion from .... – Gayashan Jan 02 '14 at 07:39
  • can you post your exact exception? – Ehsan Jan 02 '14 at 07:42
  • Error 11 The type 'YMax.POS.Printer.PrintColumnModel' cannot be used as type parameter 'T' in the generic type or method 'YMax.POS.Printer.Extensions.Clone(System.Collections.Generic.IList)'. There is no implicit reference conversion from 'YMax.POS.Printer.PrintColumnModel' to 'System.ICloneable'. G:\Tharindu\Hg\YMax.POS.Printer\YMax.POS.Printer\YMax.POS.Printer\SerialPrinter.cs 485 62 YMax.POS.Printer – Gayashan Jan 02 '14 at 07:44
  • and your call of how you are using it? – Ehsan Jan 02 '14 at 07:50
  • List tempRowModellist = new List(printRowList); tempRowModellist = printRowList.Select(i => i.Clone()).ToList(); – Gayashan Jan 02 '14 at 07:52
2

Both lists are storing references (pointers) to the same actual PrintRowModel objects. If you want to create completely separate lists, then you need to duplicate the lists and the objects being stored in the lists.

antiduh
  • 11,853
  • 4
  • 43
  • 66
2

This happens because add range copy it by reference so you change the original object!

ilay zeidman
  • 2,654
  • 5
  • 23
  • 45
0

Find out Clone() method in this SO question, this method will deserialize original object and return copy of original object,this required since making changes in reference object will affect original object.

Community
  • 1
  • 1
Ankush Madankar
  • 3,689
  • 4
  • 40
  • 74