Access and copy values in objects instead of accessing the reference to the object












-2















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.



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;
}
}


movingAverageQueue has all the same Date and Close values.










share|improve this question




















  • 1





    What is the significance of DateClose mA = sample.RemoveFromBack();? Why are you reusing this object? If keeping some values from DateClose is necessary, you'll have to implement a clone method, or make DateClose an immutable struct.

    – John
    Nov 21 '18 at 0:57








  • 4





    Is this really any different than yesterday's episode?

    – None of the Above
    Nov 21 '18 at 0:58











  • Disaffected, yesterday's question taught me I was using references instead of values. Today I am asking how can I access the values since I obviously know how to access them by reference. I am teaching myself C#.

    – Jam66125
    Nov 21 '18 at 1:16











  • John, the immutable structure sounds like the way to go. I will try it. I appreciate your response.

    – Jam66125
    Nov 21 '18 at 1:18






  • 3





    Yes and without an Minimal, Complete, and Verifiable example it is hard to tell, but I would bet you have the same problem as yesterday. In your loop, you are adding the same mA object over and over (even if one property is modified as you go).

    – None of the Above
    Nov 21 '18 at 1:20


















-2















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.



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;
}
}


movingAverageQueue has all the same Date and Close values.










share|improve this question




















  • 1





    What is the significance of DateClose mA = sample.RemoveFromBack();? Why are you reusing this object? If keeping some values from DateClose is necessary, you'll have to implement a clone method, or make DateClose an immutable struct.

    – John
    Nov 21 '18 at 0:57








  • 4





    Is this really any different than yesterday's episode?

    – None of the Above
    Nov 21 '18 at 0:58











  • Disaffected, yesterday's question taught me I was using references instead of values. Today I am asking how can I access the values since I obviously know how to access them by reference. I am teaching myself C#.

    – Jam66125
    Nov 21 '18 at 1:16











  • John, the immutable structure sounds like the way to go. I will try it. I appreciate your response.

    – Jam66125
    Nov 21 '18 at 1:18






  • 3





    Yes and without an Minimal, Complete, and Verifiable example it is hard to tell, but I would bet you have the same problem as yesterday. In your loop, you are adding the same mA object over and over (even if one property is modified as you go).

    – None of the Above
    Nov 21 '18 at 1:20
















-2












-2








-2








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.



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;
}
}


movingAverageQueue has all the same Date and Close values.










share|improve this question
















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.



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;
}
}


movingAverageQueue has all the same Date and Close values.







c# asp.net-core






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 0:52









Erik Philips

41.1k691124




41.1k691124










asked Nov 21 '18 at 0:43









Jam66125Jam66125

596




596








  • 1





    What is the significance of DateClose mA = sample.RemoveFromBack();? Why are you reusing this object? If keeping some values from DateClose is necessary, you'll have to implement a clone method, or make DateClose an immutable struct.

    – John
    Nov 21 '18 at 0:57








  • 4





    Is this really any different than yesterday's episode?

    – None of the Above
    Nov 21 '18 at 0:58











  • Disaffected, yesterday's question taught me I was using references instead of values. Today I am asking how can I access the values since I obviously know how to access them by reference. I am teaching myself C#.

    – Jam66125
    Nov 21 '18 at 1:16











  • John, the immutable structure sounds like the way to go. I will try it. I appreciate your response.

    – Jam66125
    Nov 21 '18 at 1:18






  • 3





    Yes and without an Minimal, Complete, and Verifiable example it is hard to tell, but I would bet you have the same problem as yesterday. In your loop, you are adding the same mA object over and over (even if one property is modified as you go).

    – None of the Above
    Nov 21 '18 at 1:20
















  • 1





    What is the significance of DateClose mA = sample.RemoveFromBack();? Why are you reusing this object? If keeping some values from DateClose is necessary, you'll have to implement a clone method, or make DateClose an immutable struct.

    – John
    Nov 21 '18 at 0:57








  • 4





    Is this really any different than yesterday's episode?

    – None of the Above
    Nov 21 '18 at 0:58











  • Disaffected, yesterday's question taught me I was using references instead of values. Today I am asking how can I access the values since I obviously know how to access them by reference. I am teaching myself C#.

    – Jam66125
    Nov 21 '18 at 1:16











  • John, the immutable structure sounds like the way to go. I will try it. I appreciate your response.

    – Jam66125
    Nov 21 '18 at 1:18






  • 3





    Yes and without an Minimal, Complete, and Verifiable example it is hard to tell, but I would bet you have the same problem as yesterday. In your loop, you are adding the same mA object over and over (even if one property is modified as you go).

    – None of the Above
    Nov 21 '18 at 1:20










1




1





What is the significance of DateClose mA = sample.RemoveFromBack();? Why are you reusing this object? If keeping some values from DateClose is necessary, you'll have to implement a clone method, or make DateClose an immutable struct.

– John
Nov 21 '18 at 0:57







What is the significance of DateClose mA = sample.RemoveFromBack();? Why are you reusing this object? If keeping some values from DateClose is necessary, you'll have to implement a clone method, or make DateClose an immutable struct.

– John
Nov 21 '18 at 0:57






4




4





Is this really any different than yesterday's episode?

– None of the Above
Nov 21 '18 at 0:58





Is this really any different than yesterday's episode?

– None of the Above
Nov 21 '18 at 0:58













Disaffected, yesterday's question taught me I was using references instead of values. Today I am asking how can I access the values since I obviously know how to access them by reference. I am teaching myself C#.

– Jam66125
Nov 21 '18 at 1:16





Disaffected, yesterday's question taught me I was using references instead of values. Today I am asking how can I access the values since I obviously know how to access them by reference. I am teaching myself C#.

– Jam66125
Nov 21 '18 at 1:16













John, the immutable structure sounds like the way to go. I will try it. I appreciate your response.

– Jam66125
Nov 21 '18 at 1:18





John, the immutable structure sounds like the way to go. I will try it. I appreciate your response.

– Jam66125
Nov 21 '18 at 1:18




3




3





Yes and without an Minimal, Complete, and Verifiable example it is hard to tell, but I would bet you have the same problem as yesterday. In your loop, you are adding the same mA object over and over (even if one property is modified as you go).

– None of the Above
Nov 21 '18 at 1:20







Yes and without an Minimal, Complete, and Verifiable example it is hard to tell, but I would bet you have the same problem as yesterday. In your loop, you are adding the same mA object over and over (even if one property is modified as you go).

– None of the Above
Nov 21 '18 at 1:20














1 Answer
1






active

oldest

votes


















0














I solved this of accessing by value instead of by reference with creating two new objects in the for loop, It works!



        public static Deque<DateClose> MAMethod(Queue<DateClose> queue,
Deque<DateClose> firstMASample, int period)
{
Deque<DateClose> sample = new Deque<DateClose>(firstMASample.ToArray());
Deque<DateClose> movingAverageQueue = new Deque<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.AddToBack(mA);
foreach (DateClose d in queue.ToList())
{
// create a new object for add subtraction moving averages
DateClose dateClose = new DateClose(sample.RemoveFromFront());
sub = dateClose.Close;
// create a new object to place the new moving average on the queue
DateClose mAQueueValue = new DateClose(movingAverageQueue.Last());
// subtract previous closing from new current MA

mAQueueValue.Close = mAQueueValue.Close - sub/period;
// add the new closing to new current MA
add = d;
sample.AddToBack(d);
mAQueueValue.Close = mAQueueValue.Close + add.Close/period;
mAQueueValue.Date = add.Date;
movingAverageQueue.AddToBack(mAQueueValue);
queue.Dequeue();
}

return movingAverageQueue;
}





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53403749%2faccess-and-copy-values-in-objects-instead-of-accessing-the-reference-to-the-obje%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    I solved this of accessing by value instead of by reference with creating two new objects in the for loop, It works!



            public static Deque<DateClose> MAMethod(Queue<DateClose> queue,
    Deque<DateClose> firstMASample, int period)
    {
    Deque<DateClose> sample = new Deque<DateClose>(firstMASample.ToArray());
    Deque<DateClose> movingAverageQueue = new Deque<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.AddToBack(mA);
    foreach (DateClose d in queue.ToList())
    {
    // create a new object for add subtraction moving averages
    DateClose dateClose = new DateClose(sample.RemoveFromFront());
    sub = dateClose.Close;
    // create a new object to place the new moving average on the queue
    DateClose mAQueueValue = new DateClose(movingAverageQueue.Last());
    // subtract previous closing from new current MA

    mAQueueValue.Close = mAQueueValue.Close - sub/period;
    // add the new closing to new current MA
    add = d;
    sample.AddToBack(d);
    mAQueueValue.Close = mAQueueValue.Close + add.Close/period;
    mAQueueValue.Date = add.Date;
    movingAverageQueue.AddToBack(mAQueueValue);
    queue.Dequeue();
    }

    return movingAverageQueue;
    }





    share|improve this answer




























      0














      I solved this of accessing by value instead of by reference with creating two new objects in the for loop, It works!



              public static Deque<DateClose> MAMethod(Queue<DateClose> queue,
      Deque<DateClose> firstMASample, int period)
      {
      Deque<DateClose> sample = new Deque<DateClose>(firstMASample.ToArray());
      Deque<DateClose> movingAverageQueue = new Deque<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.AddToBack(mA);
      foreach (DateClose d in queue.ToList())
      {
      // create a new object for add subtraction moving averages
      DateClose dateClose = new DateClose(sample.RemoveFromFront());
      sub = dateClose.Close;
      // create a new object to place the new moving average on the queue
      DateClose mAQueueValue = new DateClose(movingAverageQueue.Last());
      // subtract previous closing from new current MA

      mAQueueValue.Close = mAQueueValue.Close - sub/period;
      // add the new closing to new current MA
      add = d;
      sample.AddToBack(d);
      mAQueueValue.Close = mAQueueValue.Close + add.Close/period;
      mAQueueValue.Date = add.Date;
      movingAverageQueue.AddToBack(mAQueueValue);
      queue.Dequeue();
      }

      return movingAverageQueue;
      }





      share|improve this answer


























        0












        0








        0







        I solved this of accessing by value instead of by reference with creating two new objects in the for loop, It works!



                public static Deque<DateClose> MAMethod(Queue<DateClose> queue,
        Deque<DateClose> firstMASample, int period)
        {
        Deque<DateClose> sample = new Deque<DateClose>(firstMASample.ToArray());
        Deque<DateClose> movingAverageQueue = new Deque<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.AddToBack(mA);
        foreach (DateClose d in queue.ToList())
        {
        // create a new object for add subtraction moving averages
        DateClose dateClose = new DateClose(sample.RemoveFromFront());
        sub = dateClose.Close;
        // create a new object to place the new moving average on the queue
        DateClose mAQueueValue = new DateClose(movingAverageQueue.Last());
        // subtract previous closing from new current MA

        mAQueueValue.Close = mAQueueValue.Close - sub/period;
        // add the new closing to new current MA
        add = d;
        sample.AddToBack(d);
        mAQueueValue.Close = mAQueueValue.Close + add.Close/period;
        mAQueueValue.Date = add.Date;
        movingAverageQueue.AddToBack(mAQueueValue);
        queue.Dequeue();
        }

        return movingAverageQueue;
        }





        share|improve this answer













        I solved this of accessing by value instead of by reference with creating two new objects in the for loop, It works!



                public static Deque<DateClose> MAMethod(Queue<DateClose> queue,
        Deque<DateClose> firstMASample, int period)
        {
        Deque<DateClose> sample = new Deque<DateClose>(firstMASample.ToArray());
        Deque<DateClose> movingAverageQueue = new Deque<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.AddToBack(mA);
        foreach (DateClose d in queue.ToList())
        {
        // create a new object for add subtraction moving averages
        DateClose dateClose = new DateClose(sample.RemoveFromFront());
        sub = dateClose.Close;
        // create a new object to place the new moving average on the queue
        DateClose mAQueueValue = new DateClose(movingAverageQueue.Last());
        // subtract previous closing from new current MA

        mAQueueValue.Close = mAQueueValue.Close - sub/period;
        // add the new closing to new current MA
        add = d;
        sample.AddToBack(d);
        mAQueueValue.Close = mAQueueValue.Close + add.Close/period;
        mAQueueValue.Date = add.Date;
        movingAverageQueue.AddToBack(mAQueueValue);
        queue.Dequeue();
        }

        return movingAverageQueue;
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 21 '18 at 15:42









        Jam66125Jam66125

        596




        596
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53403749%2faccess-and-copy-values-in-objects-instead-of-accessing-the-reference-to-the-obje%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            How to change which sound is reproduced for terminal bell?

            Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

            Can I use Tabulator js library in my java Spring + Thymeleaf project?