Modifying the value of a key

  • Thread starter Thread starter suresh
  • Start date Start date
S

suresh

Hi all,
I have a hash table and I want to modify the value of a
key. Please let me know how can that be acheived.

Thanks
Suresh
 
What kind of "key" are you talking about? If you're talking about a
registry key, have a look at the Microsoft.Win32.Registry namespace. I need
more info before I (or anyone else) can accurately answer your question.

Chris
 
By modifying the value of a key, I assume you mean you want to change
a key of a key-value pair, leaving the value of the key-value pair the
same? You can't do that.
Remember, from MSDN Hashtable help:
"When an element is added to the Hashtable, the element is placed into
a bucket based on the hash code of the key"
So changing a key would require the element to go in a new bucket, and
there's no method to do this that I know of. Instead, you'll want to
create a new key-value pair with the same value of the old pair, and
remove the old pair.

something like (untested)
object temp = hashtable[oldkey];
hashtable.Remove(oldkey);
hashtable[newkey] = temp;

If you mean you want to change a value of a key-value pair, then this
is all you need:
hashtable[key] = newvalue;


mike
 
Back
Top