In VB .Net
For idx As Integer = 0 To HashTable.Length -1
Dim ot As ObjectType = CType(HashTable(idx), ObjectType)
Next
Main Topics
Browse All TopicsWhat's the best way to loop through a hashtable and amend the values ? For each doesn't allow updates.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
FernandoSoto: That can give some unpredictable results depending on how you are ammending the values an add can change the order of the indexing.
He must be adding/removing keys since he is getting an error on the enumerator (this code wouldn't cause a problem with a key enumerator).
Could you please put up your actual code that is failing?
Thanks,
Greg
OK! here we see the issue! You are changing the key of the record ...
myHashTable(sKey) = oRecord
This invalidates the enumerator ...
Are you actually changing the data in the key? If not you could just do this ...
For Each Item In myHashTable
oMyRecord = Item.Value
oMyRecord.MyData="New"
Next
and it won't break. If you are changing the key I can show you how to deal with that as well...
Cheers,
Greg
Ah then you can't avoid invalidating the enumerator (you are setting to a new value).
In 2.0 or higher you should not be using a hashtable though ... you should be using a Dictionary<T,V> (its like 10 times faster for structs as it doesn't require boxing/unboxing like the hashtable does).
Here is the code for use with a hashtable.
For Each Key In myHashTable.Keys
oMyRecord = myHashTable(Key)
oRecord.MyData="New"
myHashTable(Key) = oRecord
Next
You can work in a similar manner with a Dictionary ...
Cheers,
Greg
oh its because its using the one there ... make a copy of it first then do the iteration sorry ...
For Each Key In new List<object>(myHashTable.Key
oMyRecord = myHashTable(Key)
oRecord.MyData="New"
myHashTable(Key) = oRecord
Next
or
dim keys as new list(of object) (myHashTable.Keys)
For Each Key In keys
oMyRecord = myHashTable(Key)
oRecord.MyData="New"
myHashTable(Key) = oRecord
Next
as to whether it is faster where else is this data being used? if all you are ever doing is iterating over it then yes a list would be must faster.
As a side note in 2.0+ you should be using the generic versions of the objects by default. You should never be using HashTable, ArrayList, etc
Cheers,
Greg
Business Accounts
Answer for Membership
by: FernandoSotoPosted on 2009-02-04 at 09:20:07ID: 23550559
Use a for loop, this should work.
for( int idx = 0; idx < HashTable.Length; idx++)
{
ObjectType ot = (ObjectType) TashTable[idx];
}