构造ID和keyvalue对的字典

时间:2014-09-12 09:54:25

标签: c#

我是一名c#初学者 我想模特儿 唯一级别ID - > Keyvalue [String,int]

所以我做了

// Level ID -->[String][Progress] 
private Dictionary<int, KeyValuePair<string, int>> LevelProgress; 

我希望当我添加级别进度时,我只希望能够添加到当前进度,如果没有进度,则添加一个新进度。问题是我无法将任何新整数添加到“当前”键值对。

   public int AddProgress(int levelID, KeyValuePair<string, int> pair)
    {
        KeyValuePair<string, int> storedValue;
        if (LevelProgress.TryGetValue(levelID, out storedValue))
        {

        }
        else
        {
            // add progress
        }
    }

2 个答案:

答案 0 :(得分:0)

定义具有2个属性的Progress

public class Progress
{
    private string sLabel = null;
    public string Label
    {
        get { return this.sLabel; }
        set { this.sLabel = value; }
    }

    private int iValue = -1;
    public int Value 
    {
        get { return this.iValue ; }
        set { this.iValue = value; }
    }

    public Progress()
    {

    }

    public Progress(string Label, int Value)
    {
        this.sLabel = Label;
        this.iValue = Value;
    }
}

您的LevelProgress现在是包含int Key和Progress值

的Dictionary
private Dictionary<int, Progress> LevelProgress; 

像这样添加进度

public int AddProgress(int levelID, Progress P)
{
    if(LevelProgress.ContainsKey(levelID))
    {
        LevelProgress[levelID] = P;
    }
    else
    {
        LevelProgress.Add(levelID, P);
    }
}

答案 1 :(得分:0)

我从你的问题中发现你想要实现的目标并不是很清楚。以下是我认为描述你正在尝试的一些测试...

[TestFixture]
    class GameTests
    {

        [Test]
        public void AddKeyValuePair()
        {
            var levelProgress = new Dictionary<int, KeyValuePair<string, int>>();
            KeyValuePair<string, int> storedValue;
            if (levelProgress.TryGetValue(1, out storedValue))
            {
                Assert.Fail("shouldn't get here");
            }
            else
            {
                levelProgress[1] = new KeyValuePair<string, int>("something", 1);
            }

            Assert.IsTrue(levelProgress.ContainsKey(1));
            Assert.IsTrue(levelProgress[1].Equals(new KeyValuePair<string, int>("something", 1)));
        }

        [Test]
        public void UpdateKeyValuePair()
        {
            var levelProgress = new Dictionary<int, KeyValuePair<string, int>>
            {
                {1, new KeyValuePair<string, int>("something", 1)}
            };
            KeyValuePair<string, int> storedValue;
            if (levelProgress.TryGetValue(1, out storedValue))
            {
                //any operation
                levelProgress[1] = new KeyValuePair<string, int>("newness", 100);
            }
            else
            {
                Assert.Fail("shouldn't get here");
            }

            Assert.IsTrue(levelProgress.ContainsKey(1));
            Assert.IsTrue(levelProgress[1].Equals(new KeyValuePair<string, int>("something", 2)));
        }
    }

然而,它真的感觉(并且Tuples / KeyValuePairs经常向我建议)你错过了一些澄清你意图的课程。

e.g。这是一个猜测...

class Game
{
  private Dictionary<int, LevelProgress> levelsProgress;

  LevelProgress GetLevel(int i);
}

class LevelProgress
{
  void AddProgress;
}