hashtable unhandled exception

M

Mike

I am trying to utilize Hashtable to facilitate mapping one value to
another. I have successfully added several such pairs. When using the
enumerators to find keys upon subsequent manipulation, I get the
following error. Any ideas why this is? Maybe I didn't initialize the
table properly?

An unhandled exception of type 'System.InvalidOperationException'
occurred in mscorlib.dll
Additional information: Collection was modified; enumeration operation
may not execute.

My code is thusly:

if (this->m_registryKey)
{
IEnumerator* keyEnum
= this->m_stringDict->Keys->GetEnumerator();

while (keyEnum->MoveNext())
{
String* strValue;

String* strKey = __try_cast<String*>(keyEnum->Current);

if (strValue = __try_cast<String*>(
this->m_registryKey->GetValue(strKey)))
{
this->Value[strKey] = strValue;
}
else
{
this->m_registryKey->SetValue(
strKey, this->m_stringDict->Item[strKey]);
}
}
}
 
C

Carl Daniel [VC++ MVP]

Mike said:
I am trying to utilize Hashtable to facilitate mapping one value to
another. I have successfully added several such pairs. When using the
enumerators to find keys upon subsequent manipulation, I get the
following error. Any ideas why this is? Maybe I didn't initialize the
table properly?

An unhandled exception of type 'System.InvalidOperationException'
occurred in mscorlib.dll
Additional information: Collection was modified; enumeration operation
may not execute.

That exception means that the collection was modified since the enumerator
was created.

The way the .NET collection classes work is this:

1. Each collection maintains a "version number".
2. Whever an object is added to or removed from the container, the version
number is incremented.
3. When an enumerator for a collection is created, it copies the version
number of the container.
4. Whever the enumerator is used, the version number captured at creation
time is compared to the current version number of the container.
5. If the versions don't match, the exception you're seeing is thrown.

-cd
 
M

Mike

Carl Daniel said:
The way the .NET collection classes work is this:

1. Each collection maintains a "version number".
2. Whever an object is added to or removed from the container, the version
number is incremented.
3. When an enumerator for a collection is created, it copies the version
number of the container.
4. Whever the enumerator is used, the version number captured at creation
time is compared to the current version number of the container.
5. If the versions don't match, the exception you're seeing is thrown.

Makes sense. I gather Hashtable isn't such a good substitute for the
traditionally though of Map for this reason? In other words, I've
build a table of keys (above), and updating those keys with values
will increment the table version making the keys, much less its
enumerator, worthless. At least in an attempt to substitute as a Map.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top