Reputation: 1269
I have some code that downloads data into a class for storage and then creates a copy of that class to pass into a method that alters the data. Somehow my original class is also being altered and I'm not sure what I'm doing wrong.
Calculations calc = new Calculations(Symbol, Market);
calc.stockData = loadData(Symbol, Market);
for (int j = calc.stockData.Count - 8; j >= 0; j--)
{
highPrice = 0;
// 0 newest
// as you go higher in the index, it is older
for (int k = j + 1; k < j + 8; k++)
{
kIndex = k;
jIndex = j;
decimal highRiskCurrentHigh = Calculations.calculateReturnPercentageStatic(calc.stockData.ElementAtOrDefault(k).Close,
calc.stockData.ElementAtOrDefault(j).High);
if (highRiskCurrentHigh > highPrice)
{
highPrice = highRiskCurrentHigh;
highIndex = k;
}
}
Test test = new Test();
test.returnPct = highPrice;
test.date = calc.stockData.ElementAtOrDefault(highIndex).Date;
test.symbolClass = symbolsList.ElementAtOrDefault(i);
Calculations copy = calc;
test.ratingClass = performCalculations(test.symbolClass, copy, test.date); // passing a copy into the method for altering
stuffList.Add(test); // inserted a breakpoint here and the original class (calc) has been altered
}
Upvotes: 0
Views: 116
Reputation: 6920
Let's walk through your code so you understand what is happening here. (What you are telling your code to do is written in the comments.)
// 1. create a new instance of the object 'Calculations' on the heap
// 2. return the memory address of that instance and assign it to variable 'calc'
Calculations calc = new Calculations(Symbol, Market);
// 3. Create a variable called 'copy' that points to 'Calculations' type objects
// 4. Assign the value of 'calc' (which is a reference to the heap object) to 'copy'
Calculations copy = calc;
Simply put, in your code calc
and copy
are pointing to the same object in memory. That is why changes to copy
are affecting calc
. What you need to do is clone your object so that you have a new object that is equal in value but separate in memory.
Upvotes: 3
Reputation: 155608
This code does not create a copy:
Calculations copy = calc;
In C# objects all exist on the heap and are manipulated through references, compare to C++ where you can create objects on the stack and assignment is equal to a copy operation.
Instead, implement IClonable
and provide your own copy operation.
Upvotes: 5
Reputation: 7467
Calculations copy = calc;
is not creating a copy of your original data it is a reference to the same data. You need to clone this data.
More info on how to clone can be found here :
Upvotes: 3