Reputation: 87
So here's an example of someone (me) writing very bad C# code.
I'm trying to figure out the best method of storing values and replacing them with values as they become known.
Here's how I had been doing it earlier:
int
EMA_Value_Prev4,
EMA_Value_Prev3,
EMA_Value_Prev2,
EMA_Value_Prev,
EMA_Value;
EMA_Value_Prev4 = EMA_Value_Prev3;
EMA_Value_Prev3 = EMA_Value_Prev2;
EMA_Value_Prev2 = EMA_Value_Prev;
EMA_Value_Prev = EMA_Value;
EMA_Value = 0;
// In the below space, some code figures out what EMA_Value is
/* Some amazing code */
// EMA_Value now equals 245 (hypothetically).
Since then, I've used arrays to store this with for loops, like this:
int [] EMA_Value = new int [5];
for (xCount=4; xCount>1; xCount--)
{EMA_Value[xCount] = EMA_Value[xCount - 1]; }
For the way more advanced and experienced (and smarter) coder than I, what I should be doing instead that's either more efficient/elegant/process friendly?
Thanks in advance for any time or attention you give this question. :)
Upvotes: 0
Views: 30
Reputation: 2750
If the reason you're doing it that way is because you want to get the previous values in some case. You can think about using something like a Stack. .NET has a built in stack
Stack<int> stack = new Stack<int>();
stack.push(10)
int topValue = stack.pop()
Every time you get a new value, you call stack.push(newValue). If you want that the previous value, (behaving in the same way as the back button on a browser), you then use pop.
If you're using last N values for something like a runge-kutta ODE solver, than your solution with an array is as good as any other implementation
Upvotes: 1