构造ID和键值对的字典

本文关键字:字典 键值对 ID 构造 | 更新日期: 2023-09-27 18:21:42

我是一名c#初学者开发人员我想做模特唯一级别ID-->键值[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
        }
    }

构造ID和键值对的字典

从你的问题中我并不清楚你想要实现什么。以下是一些测试,我认为可以描述你正在尝试的。。。

[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经常向我建议这一点)您缺少了一些阐明您意图的类。

例如,这是一个猜测。。。

class Game
{
  private Dictionary<int, LevelProgress> levelsProgress;
  LevelProgress GetLevel(int i);
}
class LevelProgress
{
  void AddProgress;
}

定义一个具有两个属性的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值的字典

private Dictionary<int, Progress> LevelProgress; 

添加进度,如此

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