c# Queue values changing inadvertently
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
add a comment |
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
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 assignpreviousMa
in one place. It is assigned to be a reference to the last item infirstMaSample
if there is one. You are adding this same reference over and over again tomovingAverageQueue
. 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 assignpreviousMa
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
add a comment |
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
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
c# linq
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 assignpreviousMa
in one place. It is assigned to be a reference to the last item infirstMaSample
if there is one. You are adding this same reference over and over again tomovingAverageQueue
. 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 assignpreviousMa
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
add a comment |
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 assignpreviousMa
in one place. It is assigned to be a reference to the last item infirstMaSample
if there is one. You are adding this same reference over and over again tomovingAverageQueue
. 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 assignpreviousMa
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
add a comment |
1 Answer
1
active
oldest
votes
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
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
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
add a comment |
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
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
add a comment |
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
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
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 infirstMaSample
if there is one. You are adding this same reference over and over again tomovingAverageQueue
. 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 assignpreviousMa
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