C# How to access and copy values in objects instead of accessing the reference to the object

  • Thread starter Thread starter DoctorWhoKnew
  • Start date Start date
D

DoctorWhoKnew

Guest
I am attempting to populate a moving average queue but I am getting the same date and close for all of the objects in the moving average queue. I am stuck as how to not get the reference pointing to the same object instead of getting the current value in the object and placing that value on the queue. Here is the code

public class MA
{
public static Queue<DateClose> MAMethod(Queue<DateClose> queue,
Deque<DateClose> firstMASample, int period)
{
Deque<DateClose> sample = new Deque<DateClose>(firstMASample.ToArray());
Queue<DateClose> movingAverageQueue = new Queue<DateClose>(queue.Count() + 1);
// get the last item or initial MA value from the queue
DateClose mA = sample.RemoveFromBack();
DateClose dateClose = null;
decimal sub = 0;
DateClose add = null;
//put the initial Ma value on the movingAverageQueue
movingAverageQueue.
Enqueue(mA);
foreach (DateClose d in queue.ToList())
{
dateClose = sample.
RemoveFromFront();
sub = dateClose.
Close;
// subtract previous closing from new current MA
mA.
Close = mA.Close - sub/period;
// add the new closing to new current MA
add = d;
sample.
AddToBack(d);
mA.
Close = mA.Close + add.Close/period;
mA.
Date = add.Date;
movingAverageQueue.
Enqueue(mA);
queue.Dequeue();
}

return movingAverageQueue;
}
}


Continue reading...
 
Back
Top