Let's say that I have a summary collection of values ββwhere I specify the size of the collection and at any time a new value is added, all old values ββthat exceed the specified size are unloaded. Obviously (and I tested this) the best type of collection used for this behavior is the queue:
myQueue.Enqueue(newValue) If myQueue.Count > specifiedSize Then myQueue.Dequeue()
However, if I want to calculate the difference between the first and last items in the queue? Obviously, I cannot access the elements by index. But switching from a queue to something that implements IList seems to be superfluous, like writing a new Queue class. Right now I have:
Dim firstValue As Integer = myQueue.Peek() Dim lastValue As Integer = myQueue.ToArray()(myQueue.Count - 1) Dim diff As Integer = lastValue - firstValue
This ToArray() call bothers me, but an excellent alternative does not suit me. Any suggestions?
Dan tao
source share