将类传递给公式会产生相同的值

时间:2017-08-31 00:42:35

标签: c#

在下面的代码中,我有输出显示ema12的不同值(这些方法只计算ema12值,但在类列表中,每个类对列表中的最后一个ema12值具有相同的值,即使每个类都是不同的,不知何故,所有的ema12值现在都是一样的。我错过了一些简单的东西吗?

myCGImage = myUIImage.cgImage

通过Console.WriteLine()输出:

CalculationData currentCalcData = new CalculationData();

for (int i = 0; i < Data.Count; i++)
{
    var currentDate = Data.ElementAt(i).Date;

    currentCalcData = PassValuesToCalculationData(ListCalculationData, currentCalcData, 
        i, days, currentDate, PassIndicatorType.ExponentialMovingAverage);
    currentCalcData = PassValuesToCalculationData(ListCalculationData, currentCalcData, 
        i, days, currentDate, PassIndicatorType.SimpleMovingAverage);

    Console.WriteLine(currentCalcData.Ema12);

    // add current calculator class to the list
    ListCalculationData.Add(currentCalcData);
}

列表中每个类的ema12值为:47.614134621466156130843047529 47.832250005270668646730641192 47.832250005270668646730641192 48.050616671234579493833222366 48.050616671234579493833222366 48.137201115069968894655459384 48.137201115069968894655459384 48.173574299727306375368064800 48.173574299727306375368064800 48.237097726430332191985656160 48.237097726430332191985656160 48.292151362906287899720902005 48.292151362906287899720902005 48.331864514518782846424781738 48.331864514518782846424781738 48.407615912582945133568144173 48.407615912582945133568144173 48.441267124238552449092391617 48.441267124238552449092391617 48.491764841006745455880072735

这是它在PassValuesToCalculationData

中调用的方法
48.49

1 个答案:

答案 0 :(得分:2)

您遇到的问题是因为您只实例化了1个对象currentCalcData并将相同的引用多次放入ListCalculationData。

解决方案是将currentCalcData实例化放在for循环中,然后在ListCalculationData中有不同的对象。

for (int i = 0; i < Data.Count; i++)
{
    CalculationData currentCalcData = new CalculationData();
    var currentDate = Data.ElementAt(i).Date;
    //.... and continue the rest