Hi all,
I have the following code which should work. However, an exception (
InvalidOperatio nException) was thrown which doesn't make sense to me.
line 1: foreach (string name in _fieldMaps.Keys )
line 2: _fieldMaps[name] = 1;
where _fieldMaps is a hashtable containing (string, int) pairs internally.
Does anyone know why this exception is thrown? I tried debug it and found
the exception was thrown during the 2nd loop.
i.e. execution steps:
line1, line2, line1, EXCEPTION.
Thanks for any inputs here.
-Cal 6 2537
Calvin,
I would think that the second key in the hashtable is not a string, but
rather, another type. Have you debugged the code to see what the key is and
the type? If it can not be cast to a string, then this is what you will
get.
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m
"Calvin Lai" <clai[at]qdata[dot]com> wrote in message
news:uz******** ******@TK2MSFTN GP11.phx.gbl... Hi all,
I have the following code which should work. However, an exception ( InvalidOperatio nException) was thrown which doesn't make sense to me.
line 1: foreach (string name in _fieldMaps.Keys )
line 2: _fieldMaps[name] = 1;
where _fieldMaps is a hashtable containing (string, int) pairs internally.
Does anyone know why this exception is thrown? I tried debug it and found the exception was thrown during the 2nd loop. i.e. execution steps: line1, line2, line1, EXCEPTION.
Thanks for any inputs here.
-Cal
Hi Calvin,
You cannot change the collection inside foreach loop. That exception is
thrown by IEnumerator.Mov eNext method.
If you want ot modify the collection dont't use enumartaors (objects that
implement IEnumerator interface) Foreach internally uses enumerators to
iterate through collections.
Use another kind of loop. *for* and *while* both will do.
HTH
B\rgds
100
"Calvin Lai" <clai[at]qdata[dot]com> wrote in message
news:uz******** ******@TK2MSFTN GP11.phx.gbl... Hi all,
I have the following code which should work. However, an exception ( InvalidOperatio nException) was thrown which doesn't make sense to me.
line 1: foreach (string name in _fieldMaps.Keys )
line 2: _fieldMaps[name] = 1;
where _fieldMaps is a hashtable containing (string, int) pairs internally.
Does anyone know why this exception is thrown? I tried debug it and found the exception was thrown during the 2nd loop. i.e. execution steps: line1, line2, line1, EXCEPTION.
Thanks for any inputs here.
-Cal
<"Calvin Lai" <clai[at]qdata[dot]com>> wrote: I have the following code which should work.
No, it shouldn't.
However, an exception ( InvalidOperatio nException) was thrown which doesn't make sense to me.
line 1: foreach (string name in _fieldMaps.Keys )
line 2: _fieldMaps[name] = 1;
where _fieldMaps is a hashtable containing (string, int) pairs internally.
Does anyone know why this exception is thrown? I tried debug it and found the exception was thrown during the 2nd loop. i.e. execution steps: line1, line2, line1, EXCEPTION.
Thanks for any inputs here.
The key is to look at the message in the exception:
"Collection was modified; enumeration operation may not execute."
Basically, you can't modify a collection while you're enumerating it.
From the docs for IEnumerable:
<quote>
An enumerator remains valid as long as the collection remains
unchanged. If changes are made to the collection, such as adding,
modifying or deleting elements, the enumerator is irrecoverably
invalidated and the next call to MoveNext or Reset throws an
InvalidOperatio nException.
</quote>
Now, although you haven't *actually* changed the collection of keys,
you've changed the collection it's modelling (which includes the
values), which is why you're getting the problem.
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard .caspershouse.c om> wrote: I would think that the second key in the hashtable is not a string, but rather, another type. Have you debugged the code to see what the key is and the type? If it can not be cast to a string, then this is what you will get.
No - if the key wasn't a string, the OP would have got an
InvalidCastExce ption, not an InvalidOperatio nException.
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Thanks all for you input. It helps a lot. It was driving me crazy...
"100" <10*@100.com> wrote in message
news:es******** ******@TK2MSFTN GP09.phx.gbl... Hi Calvin, You cannot change the collection inside foreach loop. That exception is thrown by IEnumerator.Mov eNext method. If you want ot modify the collection dont't use enumartaors (objects that implement IEnumerator interface) Foreach internally uses enumerators to iterate through collections. Use another kind of loop. *for* and *while* both will do.
HTH B\rgds 100
"Calvin Lai" <clai[at]qdata[dot]com> wrote in message news:uz******** ******@TK2MSFTN GP11.phx.gbl... Hi all,
I have the following code which should work. However, an exception ( InvalidOperatio nException) was thrown which doesn't make sense to me.
line 1: foreach (string name in _fieldMaps.Keys )
line 2: _fieldMaps[name] = 1;
where _fieldMaps is a hashtable containing (string, int) pairs
internally. Does anyone know why this exception is thrown? I tried debug it and
found the exception was thrown during the 2nd loop. i.e. execution steps: line1, line2, line1, EXCEPTION.
Thanks for any inputs here.
-Cal
You can't alter the hashtable in a foreach loop -- the enumerator becomes
invalid, and then on your next
enum.MoveNext() (which is generated by the c# compiler from your
foreach(...) code) the InvalidOperatio nException is thrown.
What you _can_ do is place an object in the hashtable, and alter values of
that object:
class Holder{
public int SomeField;
public Holder(int val){ SomeField = val;}
}
....
Hashtable h = new Hashtable();
h.Add("bob", new Holder(1));
h.Add("charlie" , new Holder(2));
h.Add("jim", new Holder(3));
foreach(string name in h.Keys)
{
((Holder)h[name]).SomeField = 4;
}
"Calvin Lai" <clai[at]qdata[dot]com> wrote in message
news:uz******** ******@TK2MSFTN GP11.phx.gbl... Hi all,
I have the following code which should work. However, an exception ( InvalidOperatio nException) was thrown which doesn't make sense to me.
line 1: foreach (string name in _fieldMaps.Keys )
line 2: _fieldMaps[name] = 1;
where _fieldMaps is a hashtable containing (string, int) pairs internally.
Does anyone know why this exception is thrown? I tried debug it and found the exception was thrown during the 2nd loop. i.e. execution steps: line1, line2, line1, EXCEPTION.
Thanks for any inputs here.
-Cal This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Alistair Welchman |
last post by:
I have a Hashtable of ints keyed on Guids, and I want to do the following:
foreach ( DataRow row in dsWorkDays.Tables.Rows )
{
Guid PersonId = (Guid)row;
DateTime Day = (DateTime)row;
|
by: A. Burch |
last post by:
I'm wanting to use a Hashtable. I have 2 (keys, objectvalues) pairs of data
to store. I'm using a static declartion for the Hashtable the instance of
the class that is the object. If I try and retreive the first object after
storing after the 2 (key, objectvalues) are stored I always get the last
object value stored. Why doesn't qrec = (Q)HT give a copy of the
last or in this code the second of the 2 objects? How should the code be...
|
by: neovantage |
last post by:
Hey all,
I am working on a page which list categories in box format. I want to show the child categories of the parent category and the items under child category. I am using thickbox technology for showing child categories and there Items. When i clicked on the parent category it shows the record normally by listing the child categories and there items. I gave a drop down option there in child categories listing page so that if the child...
|
by: neovantage |
last post by:
Hey all,
I am working on a page which list categories in box format. I want to show the child categories of the parent category and the items under child category. I am using thickbox technology for showing child categories and there Items. When i clicked on the parent category it shows the record normally by listing the child categories and there items. I gave a drop down option there in child categories listing page so that if the child...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
| |
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |