Working with value pairs(a key and a value)
-
Hi all, I want to save pair of values, a key and a related value of it. I use HashTable to store them as follows.
private System.Collections.Hashtable hshTable = new System.Collections.Hashtable(); hshTable.Add("One", 1); hshTable.Add("Two", 2); hshTable.Add("Three", 3); hshTable.Add("One", 6);
Check that last line, I cannot do that. Actually what I want to do is, on existing entry value should be updated. How can I solve this problem. Thanks :)I appreciate your help all the time... CodingLover :)
-
Hi all, I want to save pair of values, a key and a related value of it. I use HashTable to store them as follows.
private System.Collections.Hashtable hshTable = new System.Collections.Hashtable(); hshTable.Add("One", 1); hshTable.Add("Two", 2); hshTable.Add("Three", 3); hshTable.Add("One", 6);
Check that last line, I cannot do that. Actually what I want to do is, on existing entry value should be updated. How can I solve this problem. Thanks :)I appreciate your help all the time... CodingLover :)
hshTable["One"] = 6
If there is no item with key "One" new item will be added. If there is, old value will be overwritten.Giorgi Dalakishvili #region signature My Articles Asynchronous Registry Notification Using Strongly-typed WMI Classes in .NET [^] My blog #endregion
-
Hi all, I want to save pair of values, a key and a related value of it. I use HashTable to store them as follows.
private System.Collections.Hashtable hshTable = new System.Collections.Hashtable(); hshTable.Add("One", 1); hshTable.Add("Two", 2); hshTable.Add("Three", 3); hshTable.Add("One", 6);
Check that last line, I cannot do that. Actually what I want to do is, on existing entry value should be updated. How can I solve this problem. Thanks :)I appreciate your help all the time... CodingLover :)
Hi, you can also call
Hashtable.Remove(key)
before callingHashtable.Add(key,val)
; there is no need to test for existence first. The same principle applies to the genericDictionary<T>
class. BTW:And it is similar to removing-then-adding a delegate to an event. :)Luc Pattyn [Forum Guidelines] [My Articles]
Fixturized forever. :confused:
-
Hi all, I want to save pair of values, a key and a related value of it. I use HashTable to store them as follows.
private System.Collections.Hashtable hshTable = new System.Collections.Hashtable(); hshTable.Add("One", 1); hshTable.Add("Two", 2); hshTable.Add("Three", 3); hshTable.Add("One", 6);
Check that last line, I cannot do that. Actually what I want to do is, on existing entry value should be updated. How can I solve this problem. Thanks :)I appreciate your help all the time... CodingLover :)
Use a generic
Dictionary(TKey,TValue)
. It has aContainsKey
method which will returnTRUE
if the key supplied exists. If it exist, just update the value instead of adding new one. -
Hi, you can also call
Hashtable.Remove(key)
before callingHashtable.Add(key,val)
; there is no need to test for existence first. The same principle applies to the genericDictionary<T>
class. BTW:And it is similar to removing-then-adding a delegate to an event. :)Luc Pattyn [Forum Guidelines] [My Articles]
Fixturized forever. :confused:
Thanks for replays. Actually I got data pair like One 2 and so on. I want to store each of them. If I found a duplicate one value(number) should be updated. Then later I want to take all of them to make a string. Say my data like this. One 3 Two 5 One 7 Then the string should be, One 7 Two 5
I appreciate your help all the time... CodingLover :)
-
Hi all, I want to save pair of values, a key and a related value of it. I use HashTable to store them as follows.
private System.Collections.Hashtable hshTable = new System.Collections.Hashtable(); hshTable.Add("One", 1); hshTable.Add("Two", 2); hshTable.Add("Three", 3); hshTable.Add("One", 6);
Check that last line, I cannot do that. Actually what I want to do is, on existing entry value should be updated. How can I solve this problem. Thanks :)I appreciate your help all the time... CodingLover :)
hello How can u think of storing the different value with the same "Key" As u mentioned "One"====> the key ... how it can be repeated under the same instance see link http://www.c-sharpcorner.com/UploadFile/mahesh/Hashtable11082005171748PM/Hashtable.aspx?ArticleID=6880f0d4-acc6-402c-b632-d2e353e98e62
-
Hi all, I want to save pair of values, a key and a related value of it. I use HashTable to store them as follows.
private System.Collections.Hashtable hshTable = new System.Collections.Hashtable(); hshTable.Add("One", 1); hshTable.Add("Two", 2); hshTable.Add("Three", 3); hshTable.Add("One", 6);
Check that last line, I cannot do that. Actually what I want to do is, on existing entry value should be updated. How can I solve this problem. Thanks :)I appreciate your help all the time... CodingLover :)
-
Make a custom function for it which check whether this entry is already there or not by hshTable.ContainsKey("one") if it there then remove it as hshTable.Remove("one"); and again add as hshTable.Add("One", 6);
Cheers!! Brij
Hi, create a 5-line method to do what a single statement can do? I suggest you (re)read Giorgi's and my earlier replies. :)
Luc Pattyn [Forum Guidelines] [My Articles]
Fixturized forever. :confused:
-
Thanks for replays. Actually I got data pair like One 2 and so on. I want to store each of them. If I found a duplicate one value(number) should be updated. Then later I want to take all of them to make a string. Say my data like this. One 3 Two 5 One 7 Then the string should be, One 7 Two 5
I appreciate your help all the time... CodingLover :)
Hi, you can add/replace key-value pairs as shown before. for a
Dictionary<(TKey, TValue>
you can enumerate all keys withforeach(TKey key in Dictionary.Keys) {...}
and similar stuff goes for old Hashtables. WARNING: dictionaries, hashtables don't preserve chronology, so there is no guarantee that "One 2" will come first in the enumeration. If you insist on having that, you will need a different approach, most likely a combination of a List and a Dictionary (a List preserves order unless you instruct it to drop the order, e.g. by sorting). :)Luc Pattyn [Forum Guidelines] [My Articles]
Fixturized forever. :confused:
-
Hi all, I want to save pair of values, a key and a related value of it. I use HashTable to store them as follows.
private System.Collections.Hashtable hshTable = new System.Collections.Hashtable(); hshTable.Add("One", 1); hshTable.Add("Two", 2); hshTable.Add("Three", 3); hshTable.Add("One", 6);
Check that last line, I cannot do that. Actually what I want to do is, on existing entry value should be updated. How can I solve this problem. Thanks :)I appreciate your help all the time... CodingLover :)
Hi! It quite simple If Key Not Exists Keep on Adding Else If Exists RemoveKey and then Add Thanks!
Develop2Program & Program2Develop
-
Hi! It quite simple If Key Not Exists Keep on Adding Else If Exists RemoveKey and then Add Thanks!
Develop2Program & Program2Develop
-
ok
Develop2Program & Program2Develop
-
Hi all, I want to save pair of values, a key and a related value of it. I use HashTable to store them as follows.
private System.Collections.Hashtable hshTable = new System.Collections.Hashtable(); hshTable.Add("One", 1); hshTable.Add("Two", 2); hshTable.Add("Three", 3); hshTable.Add("One", 6);
Check that last line, I cannot do that. Actually what I want to do is, on existing entry value should be updated. How can I solve this problem. Thanks :)I appreciate your help all the time... CodingLover :)
I think it is easy to solve your problem with many ways: e.g: use the combination of your key and value as new key;
-
ok
Develop2Program & Program2Develop