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
VulpesPosted Dec 20, 2011, 8:57 AM
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
}
}
BoxPosted Dec 20, 2011, 12:15 PM
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 :)
VulpesPosted Dec 20, 2011, 12:00 PM
If that's the case, you'll have to use the non-generic Queue class - there's no other (easy) alternative.
BoxPosted Dec 20, 2011, 11:43 AM
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!)
VulpesPosted Dec 20, 2011, 11:30 AM
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
BoxPosted Dec 20, 2011, 11:25 AM
VulpesPosted Dec 20, 2011, 11:04 AM
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.
BoxPosted Dec 20, 2011, 10:48 AM
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'
:|