2014-09-12 41 views
0

我交流#初學者開發 我想模擬 獨特的級別ID - >關鍵值[字符串,INT]構建ID的字典,鍵值對

所以我做了

// 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 
     } 
    } 
+1

您是否收到錯誤?向我們展示你如何「增加進度」。你是不是已經把'pair'傳遞給了它?如果你將其添加到字典中已存在的條目中,則會遇到問題,因爲如果它存在,它會滿足「if」。 – 2014-09-12 09:56:36

+0

我無法將新的鍵值對分配給存儲值。 (編譯器錯誤) – Andre 2014-09-12 09:57:59

+0

'KeyValuePair '是一個結構,你不能修改它。 – 2014-09-12 09:58:01

回答

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))); 
     } 
    } 

但是,它真的感覺(和元組/ KeyValuePairs常提示本我) 。

例如這是一個猜測...

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

    LevelProgress GetLevel(int i); 
} 

class LevelProgress 
{ 
    void AddProgress; 
} 
+0

但我不想增加一個新的storedValue.value,我想存儲新的stored.value – Andre 2014-09-12 10:15:24

+0

好吧,我更新了代碼,以便它代替KeyValuePair。 – 2014-09-12 10:18:20

+0

是它確定發言權: levelProgress [1] =新KeyValuePair (stored.key,stored.value); – Andre 2014-09-12 10:22:25

0

定義Progress類2個屬性

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鍵和前進值

private Dictionary<int, Progress> LevelProgress; 

字典添加進度喜歡這個

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

是否有可能在進度類中獲得唯一的密鑰? – Andre 2014-09-12 10:18:49

+0

「獨特的鑰匙」是什麼意思? – T00rk 2014-09-12 10:19:41

+0

確定該課程進度,標籤是可變的,我不想存儲重複的標籤..唯一的唯一 – Andre 2014-09-12 10:23:24