c# Queue values changing inadvertently












-1














The code below changes the value of the first object placed on the queue. This is the code to put the first object on the queue:



//put the initial Ma value on the movingAverageQueue
movingAverageQueue.Enqueue(previousMa)


This line of code changes the first object above I already placed on the queue



previousMa.Close = previousMa.Close - sub/period;


What am I missing in my logic for this code?



here is the full code:



public class MA
{
public static Queue<DateClose> MAMethod(Queue<DateClose> queue,
Queue<DateClose> firstMASample, int period)
{

Queue<DateClose> sample = new Queue<DateClose>(firstMASample.ToArray());
Queue<DateClose> movingAverageQueue = new Queue<DateClose>(queue.Count() + 1);
// get the last item or initial MA value from the queue
DateClose previousMa = firstMASample.LastOrDefault();
sample = new Queue<DateClose>(firstMASample.Take(firstMASample.Count - 1));
DateClose mA = null;
decimal sub = 0;
DateClose add = null;
//put the initial Ma value on the movingAverageQueue
movingAverageQueue.Enqueue(previousMa);
foreach (DateClose d in queue.ToList())

{
mA = sample.Dequeue();
sub = mA.Close;
previousMa.Close = previousMa.Close - sub/period;

add = d;
sample.Enqueue(d);
previousMa.Close = previousMa.Close + add.Close/period;
previousMa.Date = add.Date;
movingAverageQueue.Enqueue(previousMa);
queue.Dequeue();
}

return movingAverageQueue;
}
}


The DateClose class is:



public class DateClose
{
public DateTime Date { get; set; }
public decimal Close { get; set; }
}









share|improve this question




















  • 6




    "What am I missing in my logic for this code?" How about you tell us what is happening/not happening? And what you expected to happen/not happen?
    – Christopher
    Nov 19 '18 at 20:37






  • 1




    You assign previousMa in one place. It is assigned to be a reference to the last item in firstMaSample if there is one. You are adding this same reference over and over again to movingAverageQueue. You will end up with a queue where every item in the queue is the same item. Is it possible that you need to discover the difference between value types and reference types? Or have you forgotten to assign previousMa again within the loop?
    – Richardissimo
    Nov 19 '18 at 21:28










  • Richardissimo, I realized i should use a deque. I found an amazing one here, github.com/StephenCleary/Deque
    – Jam66125
    Nov 20 '18 at 13:47
















-1














The code below changes the value of the first object placed on the queue. This is the code to put the first object on the queue:



//put the initial Ma value on the movingAverageQueue
movingAverageQueue.Enqueue(previousMa)


This line of code changes the first object above I already placed on the queue



previousMa.Close = previousMa.Close - sub/period;


What am I missing in my logic for this code?



here is the full code:



public class MA
{
public static Queue<DateClose> MAMethod(Queue<DateClose> queue,
Queue<DateClose> firstMASample, int period)
{

Queue<DateClose> sample = new Queue<DateClose>(firstMASample.ToArray());
Queue<DateClose> movingAverageQueue = new Queue<DateClose>(queue.Count() + 1);
// get the last item or initial MA value from the queue
DateClose previousMa = firstMASample.LastOrDefault();
sample = new Queue<DateClose>(firstMASample.Take(firstMASample.Count - 1));
DateClose mA = null;
decimal sub = 0;
DateClose add = null;
//put the initial Ma value on the movingAverageQueue
movingAverageQueue.Enqueue(previousMa);
foreach (DateClose d in queue.ToList())

{
mA = sample.Dequeue();
sub = mA.Close;
previousMa.Close = previousMa.Close - sub/period;

add = d;
sample.Enqueue(d);
previousMa.Close = previousMa.Close + add.Close/period;
previousMa.Date = add.Date;
movingAverageQueue.Enqueue(previousMa);
queue.Dequeue();
}

return movingAverageQueue;
}
}


The DateClose class is:



public class DateClose
{
public DateTime Date { get; set; }
public decimal Close { get; set; }
}









share|improve this question




















  • 6




    "What am I missing in my logic for this code?" How about you tell us what is happening/not happening? And what you expected to happen/not happen?
    – Christopher
    Nov 19 '18 at 20:37






  • 1




    You assign previousMa in one place. It is assigned to be a reference to the last item in firstMaSample if there is one. You are adding this same reference over and over again to movingAverageQueue. You will end up with a queue where every item in the queue is the same item. Is it possible that you need to discover the difference between value types and reference types? Or have you forgotten to assign previousMa again within the loop?
    – Richardissimo
    Nov 19 '18 at 21:28










  • Richardissimo, I realized i should use a deque. I found an amazing one here, github.com/StephenCleary/Deque
    – Jam66125
    Nov 20 '18 at 13:47














-1












-1








-1







The code below changes the value of the first object placed on the queue. This is the code to put the first object on the queue:



//put the initial Ma value on the movingAverageQueue
movingAverageQueue.Enqueue(previousMa)


This line of code changes the first object above I already placed on the queue



previousMa.Close = previousMa.Close - sub/period;


What am I missing in my logic for this code?



here is the full code:



public class MA
{
public static Queue<DateClose> MAMethod(Queue<DateClose> queue,
Queue<DateClose> firstMASample, int period)
{

Queue<DateClose> sample = new Queue<DateClose>(firstMASample.ToArray());
Queue<DateClose> movingAverageQueue = new Queue<DateClose>(queue.Count() + 1);
// get the last item or initial MA value from the queue
DateClose previousMa = firstMASample.LastOrDefault();
sample = new Queue<DateClose>(firstMASample.Take(firstMASample.Count - 1));
DateClose mA = null;
decimal sub = 0;
DateClose add = null;
//put the initial Ma value on the movingAverageQueue
movingAverageQueue.Enqueue(previousMa);
foreach (DateClose d in queue.ToList())

{
mA = sample.Dequeue();
sub = mA.Close;
previousMa.Close = previousMa.Close - sub/period;

add = d;
sample.Enqueue(d);
previousMa.Close = previousMa.Close + add.Close/period;
previousMa.Date = add.Date;
movingAverageQueue.Enqueue(previousMa);
queue.Dequeue();
}

return movingAverageQueue;
}
}


The DateClose class is:



public class DateClose
{
public DateTime Date { get; set; }
public decimal Close { get; set; }
}









share|improve this question















The code below changes the value of the first object placed on the queue. This is the code to put the first object on the queue:



//put the initial Ma value on the movingAverageQueue
movingAverageQueue.Enqueue(previousMa)


This line of code changes the first object above I already placed on the queue



previousMa.Close = previousMa.Close - sub/period;


What am I missing in my logic for this code?



here is the full code:



public class MA
{
public static Queue<DateClose> MAMethod(Queue<DateClose> queue,
Queue<DateClose> firstMASample, int period)
{

Queue<DateClose> sample = new Queue<DateClose>(firstMASample.ToArray());
Queue<DateClose> movingAverageQueue = new Queue<DateClose>(queue.Count() + 1);
// get the last item or initial MA value from the queue
DateClose previousMa = firstMASample.LastOrDefault();
sample = new Queue<DateClose>(firstMASample.Take(firstMASample.Count - 1));
DateClose mA = null;
decimal sub = 0;
DateClose add = null;
//put the initial Ma value on the movingAverageQueue
movingAverageQueue.Enqueue(previousMa);
foreach (DateClose d in queue.ToList())

{
mA = sample.Dequeue();
sub = mA.Close;
previousMa.Close = previousMa.Close - sub/period;

add = d;
sample.Enqueue(d);
previousMa.Close = previousMa.Close + add.Close/period;
previousMa.Date = add.Date;
movingAverageQueue.Enqueue(previousMa);
queue.Dequeue();
}

return movingAverageQueue;
}
}


The DateClose class is:



public class DateClose
{
public DateTime Date { get; set; }
public decimal Close { get; set; }
}






c# linq






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 21:23









Adam Schiavone

1,20731853




1,20731853










asked Nov 19 '18 at 20:34









Jam66125Jam66125

596




596








  • 6




    "What am I missing in my logic for this code?" How about you tell us what is happening/not happening? And what you expected to happen/not happen?
    – Christopher
    Nov 19 '18 at 20:37






  • 1




    You assign previousMa in one place. It is assigned to be a reference to the last item in firstMaSample if there is one. You are adding this same reference over and over again to movingAverageQueue. You will end up with a queue where every item in the queue is the same item. Is it possible that you need to discover the difference between value types and reference types? Or have you forgotten to assign previousMa again within the loop?
    – Richardissimo
    Nov 19 '18 at 21:28










  • Richardissimo, I realized i should use a deque. I found an amazing one here, github.com/StephenCleary/Deque
    – Jam66125
    Nov 20 '18 at 13:47














  • 6




    "What am I missing in my logic for this code?" How about you tell us what is happening/not happening? And what you expected to happen/not happen?
    – Christopher
    Nov 19 '18 at 20:37






  • 1




    You assign previousMa in one place. It is assigned to be a reference to the last item in firstMaSample if there is one. You are adding this same reference over and over again to movingAverageQueue. You will end up with a queue where every item in the queue is the same item. Is it possible that you need to discover the difference between value types and reference types? Or have you forgotten to assign previousMa again within the loop?
    – Richardissimo
    Nov 19 '18 at 21:28










  • Richardissimo, I realized i should use a deque. I found an amazing one here, github.com/StephenCleary/Deque
    – Jam66125
    Nov 20 '18 at 13:47








6




6




"What am I missing in my logic for this code?" How about you tell us what is happening/not happening? And what you expected to happen/not happen?
– Christopher
Nov 19 '18 at 20:37




"What am I missing in my logic for this code?" How about you tell us what is happening/not happening? And what you expected to happen/not happen?
– Christopher
Nov 19 '18 at 20:37




1




1




You assign previousMa in one place. It is assigned to be a reference to the last item in firstMaSample if there is one. You are adding this same reference over and over again to movingAverageQueue. You will end up with a queue where every item in the queue is the same item. Is it possible that you need to discover the difference between value types and reference types? Or have you forgotten to assign previousMa again within the loop?
– Richardissimo
Nov 19 '18 at 21:28




You assign previousMa in one place. It is assigned to be a reference to the last item in firstMaSample if there is one. You are adding this same reference over and over again to movingAverageQueue. You will end up with a queue where every item in the queue is the same item. Is it possible that you need to discover the difference between value types and reference types? Or have you forgotten to assign previousMa again within the loop?
– Richardissimo
Nov 19 '18 at 21:28












Richardissimo, I realized i should use a deque. I found an amazing one here, github.com/StephenCleary/Deque
– Jam66125
Nov 20 '18 at 13:47




Richardissimo, I realized i should use a deque. I found an amazing one here, github.com/StephenCleary/Deque
– Jam66125
Nov 20 '18 at 13:47












1 Answer
1






active

oldest

votes


















1














In C#, object references are passed by value, and so you are enqueing a reference to that object. The reference in the queue is still pointing to the same memory location, and so when you alter the object you will see those changes when you dequeue that object reference.



Jon Skeet article on C# parameter passing






share|improve this answer























  • object references are passed by value... Perhaps not the best way of phrasing an explanation of "passing by reference" vs "passing by value".
    – Richardissimo
    Nov 19 '18 at 22:03










  • I know it sounds a bit strange at first, but that's distinct from pass by reference. The article I linked has a boxed off aside that clarifies this.
    – William Rigby
    Nov 20 '18 at 1:16











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%2f53382229%2fc-sharp-queue-values-changing-inadvertently%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









1














In C#, object references are passed by value, and so you are enqueing a reference to that object. The reference in the queue is still pointing to the same memory location, and so when you alter the object you will see those changes when you dequeue that object reference.



Jon Skeet article on C# parameter passing






share|improve this answer























  • object references are passed by value... Perhaps not the best way of phrasing an explanation of "passing by reference" vs "passing by value".
    – Richardissimo
    Nov 19 '18 at 22:03










  • I know it sounds a bit strange at first, but that's distinct from pass by reference. The article I linked has a boxed off aside that clarifies this.
    – William Rigby
    Nov 20 '18 at 1:16
















1














In C#, object references are passed by value, and so you are enqueing a reference to that object. The reference in the queue is still pointing to the same memory location, and so when you alter the object you will see those changes when you dequeue that object reference.



Jon Skeet article on C# parameter passing






share|improve this answer























  • object references are passed by value... Perhaps not the best way of phrasing an explanation of "passing by reference" vs "passing by value".
    – Richardissimo
    Nov 19 '18 at 22:03










  • I know it sounds a bit strange at first, but that's distinct from pass by reference. The article I linked has a boxed off aside that clarifies this.
    – William Rigby
    Nov 20 '18 at 1:16














1












1








1






In C#, object references are passed by value, and so you are enqueing a reference to that object. The reference in the queue is still pointing to the same memory location, and so when you alter the object you will see those changes when you dequeue that object reference.



Jon Skeet article on C# parameter passing






share|improve this answer














In C#, object references are passed by value, and so you are enqueing a reference to that object. The reference in the queue is still pointing to the same memory location, and so when you alter the object you will see those changes when you dequeue that object reference.



Jon Skeet article on C# parameter passing







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 19 '18 at 21:53

























answered Nov 19 '18 at 21:36









William RigbyWilliam Rigby

86212




86212












  • object references are passed by value... Perhaps not the best way of phrasing an explanation of "passing by reference" vs "passing by value".
    – Richardissimo
    Nov 19 '18 at 22:03










  • I know it sounds a bit strange at first, but that's distinct from pass by reference. The article I linked has a boxed off aside that clarifies this.
    – William Rigby
    Nov 20 '18 at 1:16


















  • object references are passed by value... Perhaps not the best way of phrasing an explanation of "passing by reference" vs "passing by value".
    – Richardissimo
    Nov 19 '18 at 22:03










  • I know it sounds a bit strange at first, but that's distinct from pass by reference. The article I linked has a boxed off aside that clarifies this.
    – William Rigby
    Nov 20 '18 at 1:16
















object references are passed by value... Perhaps not the best way of phrasing an explanation of "passing by reference" vs "passing by value".
– Richardissimo
Nov 19 '18 at 22:03




object references are passed by value... Perhaps not the best way of phrasing an explanation of "passing by reference" vs "passing by value".
– Richardissimo
Nov 19 '18 at 22:03












I know it sounds a bit strange at first, but that's distinct from pass by reference. The article I linked has a boxed off aside that clarifies this.
– William Rigby
Nov 20 '18 at 1:16




I know it sounds a bit strange at first, but that's distinct from pass by reference. The article I linked has a boxed off aside that clarifies this.
– William Rigby
Nov 20 '18 at 1:16


















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%2f53382229%2fc-sharp-queue-values-changing-inadvertently%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

MongoDB - Not Authorized To Execute Command

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

Npm cannot find a required file even through it is in the searched directory