views:

60

answers:

2

Hi, I would like to make a score list with only 10 elements. Basically, simple collection which adds the value. If new value is higher than other one it adds one and last one get out. (everyone knows how it looks like :) )

Secondly, I need a list of 5 lastest values that have been changed, something like history panel.

All in all, both are very similar - a list with limited items.

Is there a neat pattern for these? Some cool snippet? I need to use Silverlight for WP7 and the low power consumption solution would be great. Should I make my own collecion? Derive from one or implement interface. Thx in advance.

+2  A: 

I think System.Collections.Generic.Queue<T> is exactly what you want.

Danny Chen
for the history panel ye but how to lock the number of items to 5?and for the score example not really since I need a fast access to all elements ( remove).
lukas
@lukas: when you create the Queue, make its capacity to 5 by constructors e.g. `Queue<int> q = new Queue<int>(5);`
Danny Chen
That only sets the initial capacity, doesn't it? Or will that actually limit its capacity?
Zor
Constructor init the Capacity! doesnt make it const Queue<string> numbers = new Queue<string>(5); numbers.Enqueue("one"); numbers.Enqueue("two"); numbers.Enqueue("three"); numbers.Enqueue("four"); numbers.Enqueue("five"); numbers.Enqueue("six"); // A queue can be enumerated without disturbing its contents. foreach (string number in numbers) { Console.WriteLine(number); }
lukas
Sorry, what I mean is you can inherit `Queue<T>` to make your own queue, with special `Add()` method, where you can limit the capacity there.
Danny Chen
A: 

I did something like this to limit it to 15. It seems to be no OrderBy in WP7 :/

    public void SaveScore(ScoreInfo scoreInfo)
    {
        var listOfScoreInfo = this.GetListOrDefault<ScoreInfo>(App.SCORE);
        bool isAdd = true;
        foreach (var info in listOfScoreInfo)
        {
            if (info.Name == scoreInfo.Name && info.Score == scoreInfo.Score)
                isAdd = false;
        }
        if(isAdd)
            listOfScoreInfo.Add(scoreInfo);
        listOfScoreInfo.Sort(scoreInfo.Compare);

        if (listOfScoreInfo.Count > 15)
        {
            listOfScoreInfo.RemoveAt(15);
        }

        this.AddOrUpdateValue(App.SCORE, listOfScoreInfo);
        this.Save();
    }
lukas
OrderBy is available in windows phone, just make sure you have using System.Linq; You could use a SortedList or simplify your code by omitting the check to add the item, just add, sort, and remove if needed.
Kris