473,386 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Modifying items within a hashtable

Hi Guys

How can I modify the items within a hashtable "in situ" so to speak?

At the moment, I am locating the item that I want, extracting it,
modifying the item, deleting the hashtable item, and then adding back
the modified item to hashtable.

This seems rather long winded, and surely there must be a simpler way
than that.

Nov 4 '06 #1
2 2254
It depends on class (reference type) vs struct (value type).

For a class, you can simply obtain the reference and then make whatever
changes you need. Since there is only one object, it is immediately
reflected on the (same) object referenced from the hashtable.

For structs, this is trickier... you are given a memberwise clone, so
yes, so you would need to change the properties etc and then add it
back it - otherwise you changes disappear. Which is partly why many
structs are immutable.

Note that to do this step without having to Remove and Add (to avoid
the duplicate exception), you can use the indexer:

myDictionary[key] = value;

But note again that this isn't necessary for classes unless you wish to
change the object being referenced.

Marc

Nov 4 '06 #2
bandroo <ba*****@googlemail.comwrote:
How can I modify the items within a hashtable "in situ" so to speak?

At the moment, I am locating the item that I want, extracting it,
modifying the item, deleting the hashtable item, and then adding back
the modified item to hashtable.

This seems rather long winded, and surely there must be a simpler way
than that.
It depends:

1) Are you trying to modify the key or the value? Changing a key is
very dangerous, as a change which impacts the hashcode could render the
entry impossible to find.

2) If you're trying to modify the value, what type is the value? If
it's a reference type which lets you make the modification directly,
then you should be fine.

If it's a reference type which doesn't allow the appropriate
modification, or a value type (mutable or not, unless it's mutable via
an interface) you'll just have to change the value associated with the
key to the new one. You don't need to delete the previous value though.
Just do:

table[key] = modifiedValue;

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 4 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: John Meredith via .NET 247 | last post by:
Hi all, I used all your info to come up with the following conclusion when I needed to modify my Hashtable object. 1) Create an ArrayList from the Hashtable keys so that we have something to...
3
by: suresh | last post by:
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
0
by: choyk1 | last post by:
I intended to save properties of an object to a Hashtable. In this case, keys and values are not fixed types and I cannot use SortedList. I have no idea what order the .NET framework add items to...
6
by: VidalSasoon | last post by:
I have a singleton class that I want to only contain a hashtable. I want to be able to modify this hashtable at will. The problem I am having is each time I try to update the data using the...
4
by: Slonocode | last post by:
I have a hashtable full of objects. There seems to be no way to get an object from the hashtable and modify it. Are collections or hashtables in particular not meant for this kind of operation?
4
by: Nadav | last post by:
Hello. I have a Hashtable, to which I insert key and values. When I go over the hashtable with "foreach" later on, the items are not coming out at the order I inserted them. A quickwatch showed...
5
by: Macca | last post by:
Hi, I have a number of objects i would like to store in a list like struture. They already have ID's associated with them so i would like to utilise these ids as accessors the list.. At the...
5
by: Jen | last post by:
The usual trick to get around the "collection was modified" problem by looping through the collection backwards by index won't work with a Hashtable because there is no way to access by index,...
0
by: bloodsugarsuckerfish | last post by:
Hi all, I have in the ASP.NET cache a Hashtable of items. What seems to be happening,is that some of the actual entries in the hashtable are being removed, perhaps under memory pressure, when...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.