c# - Changing property of a value in a list without changing the value itself -


i have list values in (these types class class1). every single value has date property in it. i'm trying when 1 element gets added list:

 var entry = list.first();             list.add(entry);             list.first().dateval = new datetime(list.first().dateval.value.year,1,1);  

so i'm copying element , changing property dateval 01.01.xxxx. happens changes whole value 01.01.xxxx. happens both values. example:

value: 05.10. property: 05.10.2015

after get:

value: 01.01. property: 01.01.2015 value: 01.01. property: 01.01.2015

what want is:

value: 05.10. property: 01.01.2015 value: 05.10. property: 05.10.2015

how change property? i'm wondering why code above not work.

does work updating new objects property?

var original = list.first(); var newobject = new class1(){         dateval = original.dateval,         //set other properties     };     newobject.dateval = new datetime(newobject.dateval.value.year, 1, 1);     list.add(newobject); 

the reason both dates change when change 1 adding same object list twice, not copying it. when change 1 changes other. not sure if expected output typo suggests want value , property on single object have different value, assume value , property cannot different because same thing. using memberwiseclone() on object should clone , solve issue of both element changing date when update 1 though. also, assuming want change 1 have created changes before add instead of looking in list again , again. if want change original try this:

var original = list.first(); var newobject = new class1(){         dateval = original.dateval,         //set other properties     };     original.dateval = new datetime(original.dateval.value.year, 1, 1);     list.add(newobject); 

this clones object updates original without looking in list again.


Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -