Im unsure of how to do this. Spent most of yesterday trying various things but just ran into error after error and am now lost.
Basically I have a timer than runs every 10ms. It works out a value each time and I want to know the value it worked out 2.5 seconds ago.
My thoughts on doing this would be to have the value added to a queue once it is up to 250 I can start reading back. The logic seems easy enough to me. I just dont know how to put it into practice.
How can I 'globalise' the queue so that it isnt destroyed each time in the timer.
People suggested using a static class along the lines of
{
private static Queue gSpeed = new Queue();
public static string Speed
{
get { return gSpeed; }
set { gSpeed = value; }
}
}
but this gave all kinds of errors. static errors, conversion errors.
Can someone please point me in the right direction to go about this.
thanks

VulpesRank #55 · 29.2K reputationPosted Dec 20, 2011, 3:57 AM EST
Something like this assuming the value to be worked out is a double:
private Queue
private void timer1_Tick(object sender, EventArgs e)
{
double d = WorkoutValue();
speed.Enqueue(d);
if (speed.Count > 250)
{
double e = speed.Dequeue();
// do something with e
}
}
Box51 reputationPosted Dec 20, 2011, 5:48 AM EST
private Queue
causes errors:
queue invalid token '<' in class struct or interface member declaration c#
and when taking the out it gives an error saying:
Cannot implicitly convert type 'object' to 'double'
:|
VulpesRank #55 · 29.2K reputationPosted Dec 20, 2011, 6:04 AM EST
There's also a non-generic Queue which can take any type of value - internally it converts them all to Object which is slow if you want to store simple value types such as double, int etc.
Box51 reputationPosted Dec 20, 2011, 6:25 AM EST
VulpesRank #55 · 29.2K reputationPosted Dec 20, 2011, 6:30 AM EST
private Queue
should be:
private Queue
Notice also that if you specify the capacity - 250 or perhaps 251 here - in the constructor, then this prevents the Queue's internal array from having to be resized a number of times before 250 is reached:
private Queue
Box51 reputationPosted Dec 20, 2011, 6:43 AM EST
C:\Documents and Settings\m194012\My Documents\Visual Studio Projects\InTheDistance\Form1.cs(329): Invalid token '>' in class, struct, or interface member declaration
C:\Documents and Settings\m194012\My Documents\Visual Studio Projects\InTheDistance\Form1.cs(329): Invalid token '=' in class, struct, or interface member declaration
C:\Documents and Settings\m194012\My Documents\Visual Studio Projects\InTheDistance\Form1.cs(329): Invalid token '<' in class, struct, or interface member declaration
C:\Documents and Settings\m194012\My Documents\Visual Studio Projects\InTheDistance\Form1.cs(329): Invalid token '>' in class, struct, or interface member declaration
I just get 5 errors on that line now.
is this perhaps a version thing? im at work and they dont really like to have the latest versions of software (we still using windows xp, office 2002 and IE6!)
VulpesRank #55 · 29.2K reputationPosted Dec 20, 2011, 7:00 AM EST
If that's the case, you'll have to use the non-generic Queue class - there's no other (easy) alternative.
Box51 reputationPosted Dec 20, 2011, 7:15 AM EST
in that case i'll cast the obkects to doubles
q.Enqueue(d);
if (q.Count > 50)
{
s = (double)q.Dequeue();
}
else
{
s = (double)q.Peek();
}
s = d - s;
s *= 3600;
Well cheers for the help. its been very useful :)