Get the meaning of a word by key

How can I get the dictionary value by key in a function

my function code is this (and the command I am trying but not working):

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

my button code is

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

I want something like this:
on the XML_Array function, which will be
string xmlfile = Settings.xml

+62
dictionary c # key
Aug 29 2018-12-12T00: 00Z
source share
9 answers

It is so simple:

 String xmlfile = Data_Array["XML_File"]; 

Note that if the dictionary does not have a key equal to "XML_File" , this code throws an exception. If you want to check first, you can use TryGetValue as follows:

 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 
+84
Aug 29 2018-12-12T00: 00Z
source share

This does not work TryGetValue . It returns true or false based on whether the key is found or not, and sets its out parameter to the appropriate value if the key is.

If you want to check if there is a key or not, and do something when it is missing, you need something like this:

 bool hasValue = Data_Array.TryGetValue("XML_File", out value); if (hasValue) { xmlfile = value; } else { // do something when the value is not there } 
+19
Aug 29 2018-12-12T00: 00Z
source share

Why not just use the key name in the dictionary, C # has the following:

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

If you look at the prompt:

enter image description here

+11
Sep 29 '16 at 6:42 on
source share
 static void XML_Array(Dictionary<string, string> Data_Array) { String value; if(Data_Array.TryGetValue("XML_File", out value)) { ... Do something here with value ... } } 
+4
Aug 29 2018-12-12T00: 00Z
source share
 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
Jan 08 '14 at 18:00
source share
  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"]; } 
+2
Jan 23 '14 at 14:01
source share

I use a similar method for dasblinkenlight in a function to return a single key value from a Cookie containing a JSON array loaded into the dictionary as follows:

  /// <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"; } } 

Where "MyCookinator.Get ()" is another simple Cookie function that gets the total value of the cookie http cookie.

0
Apr 18
source share

Here is an example that I use in my source code. I get the key and value from the dictionary from element 0 to the number of elements in my dictionary. Then I populate my string [] array, which I send as a parameter after my function, which only accept params string []

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

Hope this helps you and others. This solution also works with SortedDictionary.

Respectfully,

Ozren Sirol

0
Apr 12 '17 at 7:42 on
source share
 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' 
0
05 Oct '17 at 9:31 on
source share



All Articles