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

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

    Wednesday, November 21, 2018 12:44 AM

Answers

  • Greetings DWK.

    When posting code, please don't do it via a screenshot. Use the code-insertion tool in the toolbar. That way, we can copy and modify it without having to re-type the whole lot.

    To answer your question, you need to create a new DateClose object each time through the loop and add that to the queue. I'm not sure exactly what the right coding solution is, because I don't know all the context, but it's probably something like this.

    mA.Date = add.Date;
    DateClose dc = new DateClose()
    dc.Close = mA.Close;
    dc.Date = mA.Date;
    movingAverageQueue.Enqueue(dc);

     



    Edit : Oops. I beg your pardon, that's not a screenshot. It just looks like one on my screen for some reason. Just ignore my first comment.
    • Edited by Ante Meridian Wednesday, November 21, 2018 1:05 AM Apology.
    • Marked as answer by DoctorWhoKnew Wednesday, November 21, 2018 2:00 PM
    Wednesday, November 21, 2018 1:01 AM

All replies

  • Greetings DWK.

    When posting code, please don't do it via a screenshot. Use the code-insertion tool in the toolbar. That way, we can copy and modify it without having to re-type the whole lot.

    To answer your question, you need to create a new DateClose object each time through the loop and add that to the queue. I'm not sure exactly what the right coding solution is, because I don't know all the context, but it's probably something like this.

    mA.Date = add.Date;
    DateClose dc = new DateClose()
    dc.Close = mA.Close;
    dc.Date = mA.Date;
    movingAverageQueue.Enqueue(dc);

     



    Edit : Oops. I beg your pardon, that's not a screenshot. It just looks like one on my screen for some reason. Just ignore my first comment.
    • Edited by Ante Meridian Wednesday, November 21, 2018 1:05 AM Apology.
    • Marked as answer by DoctorWhoKnew Wednesday, November 21, 2018 2:00 PM
    Wednesday, November 21, 2018 1:01 AM
  • Thanks DWK.  I will try your recommendation.  Thanks for the formatting tips.  I will use it next time.  I am fairly new to this forum, one month.  Great help from here.
    Wednesday, November 21, 2018 1:22 AM
  • Hi DoctorWhoknew,

    What is DateClose and Deque? Can you provide this part of code here? Because I can not run your code and reproduce your issue here.

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, November 21, 2018 5:43 AM
  • Cherry,

    I used Stephen Cleary's deque, which is awesome.  It does not need the initial size as the class gives a default size of  8, when the queue is full it explands.

    https://github.com/StephenCleary/Deque

    I used nuget to install it in VS2017. 

    This is DateClose.cs

    namespace myBackEnd.Models
    {
        public class DateClose
        {
            public DateTime Date { get; set; }
            public decimal Close { get; set; }
        }
    }

     

    Wednesday, November 21, 2018 10:28 AM
  • Here is the code that fully works.  Thank you so much Ante Meridian. 

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

    Wednesday, November 21, 2018 4:01 PM