2012-08-29 92 views
68

我如何通過按鍵得到函數的字典值字典值獲得通過的關鍵

我的功能代碼是這樣的(也是我嘗試,但沒有工作的命令):

static void XML_Array(Dictionary<string, string> Data_Array) 
{ 
    String xmlfile = Data_Array.TryGetValue("XML_File", out value); 
} 

我按鈕的代碼是這樣

private void button2_Click(object sender, EventArgs e) 
{ 
    Dictionary<string, string> Data_Array = new Dictionary<string, string>(); 
    Data_Array.Add("XML_File", "Settings.xml"); 

    XML_Array(Data_Array); 
} 

我想是這樣的:
XML_Array功能爲
串XMLFILE =將Settings.xml

+1

不要忘了慶祝一個答案,你的問題可接受的解決方案,如果它可以幫助你。這個'提示'也與你以前的問題有關。 http://meta.stackexchange.com/a/5235 – varg

+4

...並且不要在你的變量名稱中使用下劃線,這太難看了! – Guillaume

回答

97

它是如此簡單:

String xmlfile = Data_Array["XML_File"]; 

注意,如果字典不具有等於"XML_File"的關鍵,該代碼會拋出異常。如果你想先檢查,你可以使用TryGetValue這樣的:

string xmlfile; 
if (!Data_Array.TryGetValue("XML_File", out xmlfile)) { 
    // the key isn't in the dictionary. 
    return; // or whatever you want to do 
} 
// xmlfile is now equal to the value 
+2

酷男謝謝你!這就是我正在尋找的東西 –

20

那也不怎麼樣TryGetValue作品。根據是否找到密鑰,它返回truefalse,並且如果密鑰存在,則將其參數out設置爲相應的值。

如果你想檢查關鍵是有還是沒有,做當它失去了一些東西,你需要的是這樣的:

bool hasValue = Data_Array.TryGetValue("XML_File", out value); 
if (hasValue) { 
    xmlfile = value; 
} else { 
    // do something when the value is not there 
} 
4
static void XML_Array(Dictionary<string, string> Data_Array) 
{ 
    String value; 
    if(Data_Array.TryGetValue("XML_File", out value)) 
    { 
    ... Do something here with value ... 
    } 
} 
2
static String findFirstKeyByValue(Dictionary<string, string> Data_Array, String value) 
{ 
    if (Data_Array.ContainsValue(value)) 
    { 
     foreach (String key in Data_Array.Keys) 
     { 
      if (Data_Array[key].Equals(value)) 
       return key; 
     } 
    } 
    return null; 
} 
2
  private void button2_Click(object sender, EventArgs e) 
      { 
       Dictionary<string, string> Data_Array = new Dictionary<string, string>(); 
       Data_Array.Add("XML_File", "Settings.xml"); 

       XML_Array(Data_Array); 
      } 
      static void XML_Array(Dictionary<string, string> Data_Array) 
      { 
       String xmlfile = Data_Array["XML_File"]; 
      } 
0

我用類似的方法以dasblinkenlight的函數返回一個包含JSON數組的Cookie的單個鍵值,如下所示:

/// <summary> 
    /// Gets a single key Value from a Json filled cookie with 'cookiename','key' 
    /// </summary> 
    public static string GetSpecialCookieKeyVal(string _CookieName, string _key) 
    { 
     //CALL COOKIE VALUES INTO DICTIONARY 
     Dictionary<string, string> dictCookie = 
     JsonConvert.DeserializeObject<Dictionary<string, string>> 
     (MyCookinator.Get(_CookieName)); 

     string value; 
     if (dictCookie.TryGetValue(_key, out value)) 
     { 
      return value; 
     } 
     else 
     { 
      return "0"; 
     } 

    } 

其中「MyCookinator.Get()」是另一個獲取http cookie總值的簡單Cookie函數。

16

爲什麼不直接使用在字典的鍵名,C#有這樣的:

Dictionary<string, string> dict = new Dictionary<string, string>(); 
dict.Add("UserID", "test"); 
string userIDFromDictionaryByKey = dict["UserID"]; 

如果你看一下提示建議:

enter image description here

0

下面是例子,我在我的源使用碼。 我得到從字典從元素0到我的字典中的元素數量。然後我填我的String []數組,我作爲一個參數後,在我的函數發送其只接受PARAMS字符串[]

Dictionary<string, decimal> listKomPop = addElements(); 
    int xpopCount = listKomPop.Count; 
    if (xpopCount > 0) 
    { 
     string[] xpostoci = new string[xpopCount]; 
     for (int i = 0; i < xpopCount; i++) 
     { 
      /* here you have key and value element */ 
      string key = listKomPop.Keys.ElementAt(i); 
      decimal value = listKomPop[key]; 

      xpostoci[i] = value.ToString(); 
     } 
    ... 

希望這將幫助你和其他人。該解決方案也適用於SortedDictionary。

親切的問候,

奧茲倫Sirola

0
Dictionary<String,String> d = new Dictionary<String,String>(); 
     d.Add("1","Mahadev"); 
     d.Add("2","Mahesh"); 
     Console.WriteLine(d["1"]);// it will print Value of key '1'