2

I have one listitem and i am adding this listitem to a list multiple times with one property difference... i.e listitem have DateOfService property..k... then i am adding first item to list... it's fine and i am changing DateOfService property and adding again... but the previous added item DateOfService also changeing.... how can i overcome this problem...

sampleCode

 if (bills[index].FrequencyId == Convert.ToInt32(Frequency.Daily))
   {
    for (int day = 0; day < remainedDays; day++)
     {

       bills[index].DateOfService = DateTime.Now.Date.AddDays(day).Date;
        remainedBills.Add(bills[index]);
     }
   }

Hi i did this also but no use...

if (bills[index].FrequencyId == Convert.ToInt32(Frequency.Daily))
                    {
                        AdmissionEntryVo objAdmissionEntryVo = null;
                        for (int day = 0; day < remainedDays; day++)
                        {
                            objAdmissionEntryVo = new AdmissionEntryVo();
                            objAdmissionEntryVo = bills[index];
                            objAdmissionEntryVo.DateOfService = DateTime.Now.Date.AddDays(day).Date;
                            remainedBills.Add(objAdmissionEntryVo);
                        }
                    }
Venki Chikkanti
  • 103
  • 2
  • 11

4 Answers4

1

They're all the same object, you're not adding multiple ones. The only way to fix what you're talking about is to create new instances each time you add.

Paul
  • 35,689
  • 11
  • 93
  • 122
1

i am changing DateOfService property and adding again... but the previous added item DateOfService also changeing

That is because you are adding an object reference to the list, and your objects in the list are pointing to the same reference, so when you are changing an item you are seeing the effect in the others. You need to create a new instance of your object and then modify its property and add it to the list.

Habib
  • 219,104
  • 29
  • 407
  • 436
  • @VenkiChikkanti, your line `objAdmissionEntryVo = bills[index];` is again copying the reference. You need to copy/map the properties to the the new object or implement a Copy method on your object – Habib Dec 12 '12 at 05:48
0

Seems it relates to the "Preference and Primitive data type". Since all of those objects are just the same object, so you keep adding the same object to your list. My advice is to learn and distinguish "Preference and Primitive data types" before you continue. ;) Cheers.

Toby D
  • 1,465
  • 1
  • 19
  • 31
0

Hi all i've copied all properties to another listitem by writing extention method and added... it's working fine...

Here is extention method

public static void CopyPropertiesTo<T>(this T source, T dest)
        {
            var plist = from prop in typeof(T).GetProperties() where prop.CanRead && prop.CanWrite select prop;

            foreach (PropertyInfo prop in plist)
            {
                prop.SetValue(dest, prop.GetValue(source, null), null);
            }
        }
Venki Chikkanti
  • 103
  • 2
  • 11