I have a couple of hash tables that I need to be able to copy the values betwwen without modifying the key collection.
For example, here's some code snippets:
Dim dctMemberAttributes As New HashTable
Other code loads the hash-table dctMemberAttributes with data
Function GetMassiveData(ByRef dctAttributes As HashTable) As Boolean
Dim strAttribute As String
For Each strAttribute In dctAttributes.Keys
'Do a bunch of stuff, ifs, loops, etc.
'Deep inside one codition, there's code like this
dctAttributes(strAttribute) = dctMemberAttributes(strAttribute)
Next
End Function
When you execute this code, you get a NASTY error at the "Next" line: "Collection was modified; enumeration operation may not execute."[:O]
OH, BY THE WAY! This code was ported from classic ASP and functioned perfectly there![:@]
I think the problem is that the assignment function is not just copying in the value, but is copying in the key as well. How do I just copy the vale and leave the key/collection alone?
Appearantly, ANY change to the hashtable renders the foreach impossible to complete. So, how the hell am I supposed to do this now?[8o|] I need to copy the values from the dctMemberAttributes hash into the matching keys in dctAttributes. I haven't found
a way to loop through a hash table by integer index...?
ojm37
Contributor
2248 Points
832 Posts
Hashtable HowTo?
Feb 15, 2006 04:19 PM|LINK
I have a couple of hash tables that I need to be able to copy the values betwwen without modifying the key collection.
For example, here's some code snippets:
Dim dctMemberAttributes As New HashTable
Other code loads the hash-table dctMemberAttributes with data
Function GetMassiveData(ByRef dctAttributes As HashTable) As Boolean
Dim strAttribute As String
For Each strAttribute In dctAttributes.Keys
'Do a bunch of stuff, ifs, loops, etc.
'Deep inside one codition, there's code like this
dctAttributes(strAttribute) = dctMemberAttributes(strAttribute)
Next
End Function
When you execute this code, you get a NASTY error at the "Next" line: "Collection was modified; enumeration operation may not execute."[:O]
OH, BY THE WAY! This code was ported from classic ASP and functioned perfectly there![:@]
I think the problem is that the assignment function is not just copying in the value, but is copying in the key as well. How do I just copy the vale and leave the key/collection alone?
Ideas?
TIA,
ojm37
Contributor
2248 Points
832 Posts
Re: Hashtable HowTo?
Feb 15, 2006 04:44 PM|LINK
Appearantly, ANY change to the hashtable renders the foreach impossible to complete. So, how the hell am I supposed to do this now?[8o|] I need to copy the values from the dctMemberAttributes hash into the matching keys in dctAttributes. I haven't found a way to loop through a hash table by integer index...?
Anyone?
DMW
All-Star
15943 Points
2353 Posts
Re: Hashtable HowTo?
Feb 15, 2006 05:16 PM|LINK
Try the following. given
Dim keys(dctAttributes.Keys.Count) As String
ht.Keys.CopyTo( keys, 0 )
foreach s as string in keys
dctAttributes{s} = dctMemberAttributes(s)
Next
This is a simple technique to copy the keys into an array, so that you're now foreaching through the array, not the hashtable.
Dave
ojm37
Contributor
2248 Points
832 Posts
Re: Hashtable HowTo?
Feb 15, 2006 05:20 PM|LINK
Thanks. What a pain in the [:D]. I had coded it to copy the keys into an array with a loop, so the CopyTo syntax is nice.
Cheers,