I’m getting mad with this piece of code:
for (int h = 0; h < invocations; h++)
{
Filter newFilter = new Filter();
newFilter = customFilter;
newFilter.DateFrom = customFilter.DateFrom.AddDays(h*8);
newFilter.DateTo = newFilter.DateTo.AddDays(8).AddSeconds(-1);
customFilter is an object of type Filter
customFilter is as an object which has about 10 properties inside it correctly filled. I need to create a new object but I’ve to change just two of these 10: DateFrom and DateTo.
The problem is that after the assignment of newFilter.DateFrom and newFilter.DateTo, also the customFilter.DateFrom and customFilter.DateTo change.
Could someone help me?
Bye
M.
newFiltersimply a variable. I’m assumingFilteris a class; in which case, when you do this:you can forget about the
new Filter()– you’ve just dropped that somewhere on the floor, inaccessible from anwhere: you have setnewFilterto be a reference to the existing object, via the existing referencecustomFilter.newFilterandcustomFilterare now references to the same object.I suspect you just need to remove the line:
leaving just:
or make it cloneable: