Hi all,

I get InvalidOperationException while using foreach-loop.
It is not a rule, it just happens in some parts of code, in some - not.
As soon as I replace the foreach-loop by for-loop no Exception is thrown...

What can it be I wonder?

Thanks in advance

Re: foreach-loop bug in CF2.0? by ctacke/>

ctacke/>
Wed Jan 23 13:09:26 CST 2008

Well you've given us pretty much no useful information to go on. My guess
is it's a bug in *your* iterator, not the CF.


--

Chris Tacke, eMVP
Join the Embedded Developer Community
http://community.opennetcf.com


"c_xyTopa" <a.dratchnev@runtime.de> wrote in message
news:enVDz7eXIHA.1184@TK2MSFTNGP04.phx.gbl...
> Hi all,
>
> I get InvalidOperationException while using foreach-loop.
> It is not a rule, it just happens in some parts of code, in some - not.
> As soon as I replace the foreach-loop by for-loop no Exception is
> thrown...
>
> What can it be I wonder?
>
> Thanks in advance
>



Re: foreach-loop bug in CF2.0? by Jesse

Jesse
Wed Jan 23 13:16:32 CST 2008

Hello c_xyTopa,

> Hi all,
>
> I get InvalidOperationException while using foreach-loop.
> It is not a rule, it just happens in some parts of code, in some -
> not.
> As soon as I replace the foreach-loop by for-loop no Exception is
> thrown...
> What can it be I wonder?

If you were to post the code inside the loop we might be able to figure it
out...


--
Jesse Houwing
jesse.houwing at sogeti.n



Re: foreach-loop bug in CF2.0? by c_xyTopa

c_xyTopa
Wed Jan 23 16:30:06 CST 2008

This is a multi-part message in MIME format.

------=_NextPart_000_0013_01C85E17.E341FAF0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

Sorry...
For example here. This is an event-handle of the ui-layer to delete =
customer.
The _customerManager deletes customer from the list in the data-layer.
The _currentList is the reference to the list of the data-layer.
That means that the _currentList has one element less after that and =
actualy there is no need for the loop to delete the element for the =
second time...

List<Customer> _currentList =3D _customerManager.GetAll();

void view_DeleteCustomer(object sender, CustomerEventArgs e)
{
if (e.Customer =3D=3D null)
return;
Customer deleted =3D e.Customer;
deleted.Status =3D CustomerStatus.Deleted;
_customerManager.Delete(deleted);
// remove from the current list
foreach (Customer c in _currentList)=20
{
if (c.Guid.CompareTo(deleted.Guid) =3D=3D 0)
_currentList.Remove(c);
}
view.UpdateCustomerList(_currentList);
}


and a stack... The VS also points on keyword "in" of the loop.

bei System.ThrowHelper.ThrowInvalidOperationException()
at Enumerator.MoveNext()
bei =
Runtime.HakaMobile.Modules.CustomerModule.CustomersList.CustomerListPrese=
nter.view_DeleteCustomer()
bei =
Runtime.HakaMobile.Modules.CustomerModule.CustomersList.CustomerListView.=
DeleteCustomerMenuItem_Click()
bei System.Windows.Forms.MenuItem.OnClick()
bei System.Windows.Forms.Menu.ProcessMnuProc()
bei System.Windows.Forms.Form.WnProc()
bei System.Windows.Forms.Control._InternalWnProc()
bei Microsoft.AGL.Forms.EVL.EnterMainLoop()
bei System.Windows.Forms.Application.Run()
bei =
Microsoft.Practices.Mobile.CompositeUI.WinForms.FormShellApplication`2.St=
art()
bei Microsoft.Practices.Mobile.CompositeUI.CabApplication`1.Run()
bei Runtime.HakaMobile.Shell.ShellApplication.Main()









"<ctacke/>" <ctacke[at]opennetcf[dot]com> schrieb im Newsbeitrag =
news:edvO7NfXIHA.2268@TK2MSFTNGP02.phx.gbl...
> Well you've given us pretty much no useful information to go on. My =
guess=20
> is it's a bug in *your* iterator, not the CF.
>=20
>=20
> --=20
>=20
> Chris Tacke, eMVP
> Join the Embedded Developer Community
> http://community.opennetcf.com
>=20
>=20
> "c_xyTopa" <a.dratchnev@runtime.de> wrote in message=20
> news:enVDz7eXIHA.1184@TK2MSFTNGP04.phx.gbl...
>> Hi all,
>>
>> I get InvalidOperationException while using foreach-loop.
>> It is not a rule, it just happens in some parts of code, in some - =
not.
>> As soon as I replace the foreach-loop by for-loop no Exception is=20
>> thrown...
>>
>> What can it be I wonder?
>>
>> Thanks in advance
>>=20
>=20
>
------=_NextPart_000_0013_01C85E17.E341FAF0
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 6.00.6000.16587" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY><FONT color=3D#0000ff size=3D2>
<DIV align=3Djustify><FONT face=3DArial =
color=3D#000000>Sorry...</FONT></DIV>
<DIV align=3Djustify><FONT face=3DArial color=3D#000000>For example =
here.&nbsp;This is=20
an event-handle of the ui-layer to delete customer.</FONT></DIV>
<DIV align=3Djustify><FONT face=3DArial color=3D#000000>The <FONT=20
face=3D"Times New Roman">_</FONT><FONT face=3D"Courier =
New">customerManager=20
</FONT><FONT face=3DArial>deletes customer from the list in the=20
data-layer.</FONT></FONT></DIV>
<DIV align=3Djustify><FONT face=3DArial color=3D#000000><FONT =
face=3DArial><FONT=20
face=3D"Courier New"><FONT face=3DArial>The</FONT>=20
_currentList</FONT>&nbsp;is&nbsp;the reference&nbsp;to the list of the=20
data-layer.</FONT></FONT></DIV>
<DIV align=3Djustify><FONT face=3DArial color=3D#000000>That means that =
the <FONT=20
face=3D"Courier New">_currentList </FONT><FONT face=3DArial>has one =
element less=20
after that and actualy there is no need for the loop to delete the =
element for=20
the second time...</FONT></FONT></DIV>
<DIV align=3Djustify><FONT face=3DArial =
color=3D#000000></FONT>&nbsp;</DIV><FONT=20
color=3D#0000ff size=3D2>
<P align=3Djustify></FONT><FONT face=3D"Courier New"><FONT =
color=3D#2b91af=20
size=3D2>List</FONT><FONT color=3D#000000 size=3D2>&lt;</FONT><FONT =
color=3D#2b91af=20
size=3D2>Customer</FONT><FONT size=3D2><FONT color=3D#000000>&gt; =
_currentList =3D <FONT=20
size=3D2>_customerManager.GetAll();</FONT></FONT></FONT></FONT></P>
<DIV><FONT face=3D"Courier New">void</FONT></FONT><FONT face=3D"Courier =
New"><FONT=20
size=3D2> view_DeleteCustomer(</FONT><FONT color=3D#0000ff =
size=3D2>object</FONT><FONT=20
size=3D2> sender, </FONT><FONT color=3D#2b91af=20
size=3D2>CustomerEventArgs</FONT></FONT><FONT size=3D2><FONT =
face=3D"Courier New">=20
e)</FONT></FONT></DIV>
<DIV><FONT size=3D2><FONT face=3D"Courier New">{</FONT></DIV>
<DIV></FONT><FONT face=3D"Courier New"><FONT color=3D#0000ff=20
size=3D2>&nbsp;&nbsp;&nbsp; if</FONT><FONT size=3D2> (e.Customer =3D=3D =
</FONT><FONT=20
color=3D#0000ff size=3D2>null</FONT></FONT><FONT size=3D2><FONT=20
face=3D"Courier New">)</FONT></DIV>
<DIV align=3Djustify></FONT><FONT face=3D"Courier New" color=3D#0000ff=20
size=3D2>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return</FONT><FONT =
size=3D2><FONT=20
face=3D"Courier New">;</FONT></DIV>
<DIV align=3Djustify></FONT><FONT face=3D"Courier New" color=3D#2b91af=20
size=3D2>&nbsp;&nbsp;&nbsp; Customer</FONT><FONT size=3D2><FONT =
face=3D"Courier New">=20
deleted =3D e.Customer;</FONT></DIV>
<DIV align=3Djustify><FONT face=3D"Courier New">&nbsp;&nbsp;&nbsp; =
deleted.Status =3D=20
</FONT></FONT><FONT face=3D"Courier New" color=3D#2b91af=20
size=3D2>CustomerStatus</FONT><FONT size=3D2><FONT=20
face=3D"Courier New">.Deleted;</FONT></DIV>
<DIV align=3Djustify><FONT face=3D"Courier New">&nbsp;&nbsp;&nbsp;=20
_customerManager.Delete(deleted);</FONT></DIV>
<DIV align=3Djustify></FONT><FONT color=3D#008000 size=3D2><FONT=20
face=3D"Courier New">&nbsp;&nbsp;&nbsp; // remove from the current=20
list</FONT></DIV></FONT><FONT size=3D2>
<DIV align=3Djustify></FONT><FONT face=3D"Courier New"><FONT =
color=3D#0000ff=20
size=3D2>&nbsp;&nbsp;&nbsp; foreach</FONT><FONT size=3D2> (</FONT><FONT=20
color=3D#2b91af size=3D2>Customer</FONT><FONT size=3D2> c </FONT><FONT =
color=3D#0000ff=20
size=3D2>in</FONT></FONT><FONT size=3D2><FONT face=3D"Courier New"> =
_currentList)=20
</FONT></DIV>
<DIV align=3Djustify><FONT face=3D"Courier New">&nbsp;&nbsp;&nbsp; =
{</FONT></DIV>
<DIV align=3Djustify></FONT><FONT face=3D"Courier New" color=3D#0000ff=20
size=3D2>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if</FONT><FONT =
size=3D2><FONT=20
face=3D"Courier New"> (c.Guid.CompareTo(deleted.Guid) =3D=3D =
0)</FONT></DIV>
<DIV align=3Djustify><FONT face=3D"Courier New">&nbsp;&nbsp;&nbsp;=20
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; =
_currentList.Remove(c);</FONT></DIV>
<DIV align=3Djustify><FONT face=3D"Courier New">&nbsp;&nbsp;&nbsp; =
}</FONT></DIV>
<DIV align=3Djustify><FONT face=3D"Courier New">&nbsp;&nbsp;&nbsp;=20
view.UpdateCustomerList(_currentList);</FONT></DIV>
<DIV align=3Djustify><FONT face=3D"Courier New">}</FONT></DIV>
<DIV align=3Djustify><FONT face=3DArial></FONT>&nbsp;</DIV>
<DIV align=3Djustify><FONT face=3DArial></FONT>&nbsp;</DIV>
<DIV align=3Djustify><FONT face=3DArial>and a stack... The VS also =
points on keyword=20
"in" of the loop.</FONT></DIV>
<DIV align=3Djustify><FONT face=3DArial></FONT>&nbsp;</DIV>
<DIV align=3Djustify><FONT face=3DArial>bei=20
System.ThrowHelper.ThrowInvalidOperationException()<BR>at=20
Enumerator.MoveNext()<BR>bei=20
Runtime.HakaMobile.Modules.CustomerModule.CustomersList.CustomerListPrese=
nter.view_DeleteCustomer()<BR>bei=20
Runtime.HakaMobile.Modules.CustomerModule.CustomersList.CustomerListView.=
DeleteCustomerMenuItem_Click()<BR>bei=20
System.Windows.Forms.MenuItem.OnClick()<BR>bei=20
System.Windows.Forms.Menu.ProcessMnuProc()<BR>bei=20
System.Windows.Forms.Form.WnProc()<BR>bei=20
System.Windows.Forms.Control._InternalWnProc()<BR>bei=20
Microsoft.AGL.Forms.EVL.EnterMainLoop()<BR>bei=20
System.Windows.Forms.Application.Run()<BR>bei=20
Microsoft.Practices.Mobile.CompositeUI.WinForms.FormShellApplication`2.St=
art()<BR>bei=20
Microsoft.Practices.Mobile.CompositeUI.CabApplication`1.Run()<BR>bei=20
Runtime.HakaMobile.Shell.ShellApplication.Main()<BR></FONT></DIV>
<DIV align=3Djustify><FONT face=3DArial></FONT>&nbsp;</DIV>
<DIV align=3Djustify><FONT face=3DArial></FONT>&nbsp;</DIV>
<DIV align=3Djustify><FONT face=3DArial>&nbsp;</DIV></FONT>
<DIV align=3Djustify><FONT face=3DArial></FONT>&nbsp;</DIV>
<DIV align=3Djustify><FONT face=3DArial></FONT>&nbsp;</DIV>
<DIV align=3Djustify><FONT face=3DArial></FONT>&nbsp;</DIV>
<DIV align=3Djustify><FONT face=3DArial></FONT>&nbsp;</DIV>
<DIV align=3Djustify><FONT face=3DArial></FONT>&nbsp;</DIV></FONT>
<DIV>"&lt;ctacke/&gt;" &lt;ctacke[at]opennetcf[dot]com&gt; schrieb im=20
Newsbeitrag <A=20
href=3D"news:edvO7NfXIHA.2268@TK2MSFTNGP02.phx.gbl">news:edvO7NfXIHA.2268=
@TK2MSFTNGP02.phx.gbl</A>...</DIV>&gt;=20
Well you've given us pretty much no useful information to go on.&nbsp; =
My guess=20
<BR>&gt; is it's a bug in *your* iterator, not the CF.<BR>&gt; <BR>&gt; =
<BR>&gt;=20
-- <BR>&gt; <BR>&gt; Chris Tacke, eMVP<BR>&gt; Join the Embedded =
Developer=20
Community<BR>&gt; <A=20
href=3D"http://community.opennetcf.com">http://community.opennetcf.com</A=
><BR>&gt;=20
<BR>&gt; <BR>&gt; "c_xyTopa" &lt;<A=20
href=3D"mailto:a.dratchnev@runtime.de">a.dratchnev@runtime.de</A>&gt; =
wrote in=20
message <BR>&gt; <A=20
href=3D"news:enVDz7eXIHA.1184@TK2MSFTNGP04.phx.gbl">news:enVDz7eXIHA.1184=
@TK2MSFTNGP04.phx.gbl</A>...<BR>&gt;&gt;=20
Hi all,<BR>&gt;&gt;<BR>&gt;&gt; I get InvalidOperationException while =
using=20
foreach-loop.<BR>&gt;&gt; It is not a rule, it just happens in some =
parts of=20
code, in some - not.<BR>&gt;&gt; As soon as I replace the foreach-loop =
by=20
for-loop no Exception is <BR>&gt;&gt; thrown...<BR>&gt;&gt;<BR>&gt;&gt; =
What can=20
it be I wonder?<BR>&gt;&gt;<BR>&gt;&gt; Thanks in advance<BR>&gt;&gt; =
<BR>&gt;=20
<BR>&gt;</BODY></HTML>

------=_NextPart_000_0013_01C85E17.E341FAF0--


Re: foreach-loop bug in CF2.0? by joker

joker
Wed Jan 23 16:50:28 CST 2008

Chris was right (suprise), there is a bug in your iterator. You can't
remove items from a list that you're enumerating.

The message of the exception you got should have been:

"Collection was modified; enumeration operation may not execute."

You're code will have to be smarter.

On Jan 23, 5:30=A0pm, "c_xyTopa" <a.dratch...@runtime.de> wrote:
> Sorry...
> For example here. This is an event-handle of the ui-layer to delete custom=
er.
> The _customerManager deletes customer from the list in the data-layer.
> The _currentList is the reference to the list of the data-layer.
> That means that the _currentList has one element less after that and actua=
ly there is no need for the loop to delete the element for the second time..=
.
>
> List<Customer> _currentList =3D _customerManager.GetAll();
>
> void view_DeleteCustomer(object sender, CustomerEventArgs e)
> {
> =A0 =A0 if (e.Customer =3D=3D null)
> =A0 =A0 =A0 =A0 return;
> =A0 =A0 Customer deleted =3D e.Customer;
> =A0 =A0 deleted.Status =3D CustomerStatus.Deleted;
> =A0 =A0 _customerManager.Delete(deleted);
> =A0 =A0 // remove from the current list
> =A0 =A0 foreach (Customer c in _currentList)
> =A0 =A0 {
> =A0 =A0 =A0 =A0 if (c.Guid.CompareTo(deleted.Guid) =3D=3D 0)
> =A0 =A0 =A0 =A0 =A0 =A0 _currentList.Remove(c);
> =A0 =A0 }
> =A0 =A0 view.UpdateCustomerList(_currentList);
>
> }
>
> and a stack... The VS also points on keyword "in" of the loop.
>
> bei System.ThrowHelper.ThrowInvalidOperationException()
> at Enumerator.MoveNext()
> bei Runtime.HakaMobile.Modules.CustomerModule.CustomersList.CustomerListPr=
esent=ADer.view_DeleteCustomer()
> bei Runtime.HakaMobile.Modules.CustomerModule.CustomersList.CustomerListVi=
ew.De=ADleteCustomerMenuItem_Click()
> bei System.Windows.Forms.MenuItem.OnClick()
> bei System.Windows.Forms.Menu.ProcessMnuProc()
> bei System.Windows.Forms.Form.WnProc()
> bei System.Windows.Forms.Control._InternalWnProc()
> bei Microsoft.AGL.Forms.EVL.EnterMainLoop()
> bei System.Windows.Forms.Application.Run()
> bei Microsoft.Practices.Mobile.CompositeUI.WinForms.FormShellApplication`2=
.Star=ADt()
> bei Microsoft.Practices.Mobile.CompositeUI.CabApplication`1.Run()
> bei Runtime.HakaMobile.Shell.ShellApplication.Main()
>
> "<ctacke/>" <ctacke[at]opennetcf[dot]com> schrieb im Newsbeitragnews:edvO7=
NfXIHA.2268@TK2MSFTNGP02.phx.gbl...> Well you've given us pretty much no use=
ful information to go on. =A0My guess
> > is it's a bug in *your* iterator, not the CF.
>
> > --
>
> > Chris Tacke, eMVP
> > Join the Embedded Developer Community
> >http://community.opennetcf.com
>
> > "c_xyTopa" <a.dratch...@runtime.de> wrote in message
> >news:enVDz7eXIHA.1184@TK2MSFTNGP04.phx.gbl...
> >> Hi all,
>
> >> I get InvalidOperationException while using foreach-loop.
> >> It is not a rule, it just happens in some parts of code, in some - not.=

> >> As soon as I replace the foreach-loop by for-loop no Exception is
> >> thrown...
>
> >> What can it be I wonder?
>
> >> Thanks in advance


Re: foreach-loop bug in CF2.0? by Jin

Jin
Wed Jan 23 16:52:39 CST 2008

On Jan 23, 5:30 pm, "c_xyTopa" <a.dratch...@runtime.de> wrote:
> Sorry...
> For example here. This is an event-handle of the ui-layer to delete customer.
> The _customerManager deletes customer from the list in the data-layer.
> The _currentList is the reference to the list of the data-layer.
> That means that the _currentList has one element less after that and actualy there is no need for the loop to delete the element for the second time...
>
> List<Customer> _currentList = _customerManager.GetAll();
>
> void view_DeleteCustomer(object sender, CustomerEventArgs e)
> {
> if (e.Customer == null)
> return;
> Customer deleted = e.Customer;
> deleted.Status = CustomerStatus.Deleted;
> _customerManager.Delete(deleted);
> // remove from the current list
> foreach (Customer c in _currentList)
> {
> if (c.Guid.CompareTo(deleted.Guid) == 0)
> _currentList.Remove(c);
> }
> view.UpdateCustomerList(_currentList);
>
> }
>
> and a stack... The VS also points on keyword "in" of the loop.
>
> bei System.ThrowHelper.ThrowInvalidOperationException()
> at Enumerator.MoveNext()
> bei Runtime.HakaMobile.Modules.CustomerModule.CustomersList.CustomerListPresenter.view_DeleteCustomer()
> bei Runtime.HakaMobile.Modules.CustomerModule.CustomersList.CustomerListView.DeleteCustomerMenuItem_Click()
> bei System.Windows.Forms.MenuItem.OnClick()
> bei System.Windows.Forms.Menu.ProcessMnuProc()
> bei System.Windows.Forms.Form.WnProc()
> bei System.Windows.Forms.Control._InternalWnProc()
> bei Microsoft.AGL.Forms.EVL.EnterMainLoop()
> bei System.Windows.Forms.Application.Run()
> bei Microsoft.Practices.Mobile.CompositeUI.WinForms.FormShellApplication`2.Start()
> bei Microsoft.Practices.Mobile.CompositeUI.CabApplication`1.Run()
> bei Runtime.HakaMobile.Shell.ShellApplication.Main()
>
> "<ctacke/>" <ctacke[at]opennetcf[dot]com> schrieb im Newsbeitragnews:edvO7NfXIHA.2268@TK2MSFTNGP02.phx.gbl...
>
> > Well you've given us pretty much no useful information to go on. My guess
> > is it's a bug in *your* iterator, not the CF.
>
> > --
>
> > Chris Tacke, eMVP
> > Join the Embedded Developer Community
> >http://community.opennetcf.com
>
> > "c_xyTopa" <a.dratch...@runtime.de> wrote in message
> >news:enVDz7eXIHA.1184@TK2MSFTNGP04.phx.gbl...
> >> Hi all,
>
> >> I get InvalidOperationException while using foreach-loop.
> >> It is not a rule, it just happens in some parts of code, in some - not.
> >> As soon as I replace the foreach-loop by for-loop no Exception is
> >> thrown...
>
> >> What can it be I wonder?
>
> >> Thanks in advance

You're removing the list element while looping. This can cause this.

- Jin

Re: foreach-loop bug in CF2.0? by srhartone

srhartone
Wed Jan 23 17:03:01 CST 2008

That is no suprise really after looking at your code, as you are iterating
the collection that you are deleting from. So the MoveNext() will be at some
point pointing to an invalid reference and not the reference that you
intended. Basically when you call MoveNext (which is what foreach will do)
this moves a cursor onwards, an index, then when you read the item, calling
IEnumerable.Current the enumerator will check that cursor is not greater than
the list of the collection, if it is, then an InvalidOperationException will
be thrown. Because you are deleting them, this count will not be what it was
when the iterator started. The code will look a little something like this:

object IEnumerator.Current
{
get
{
if ((cursor < 0) || (cursor == mycoll.Length))
throw new InvalidOperationException();
return mycoll[cursor];
}
}

As you can see, your code will break the above.

Copy the items you want to delete into a separate collection maybe a
dictionary, then remove by id or something like that, just don't do it in
your iterator.
--
Simon Hart
Visual Developer - Device Application Development MVP
http://simonrhart.blogspot.com


"c_xyTopa" wrote:

> Sorry...
> For example here. This is an event-handle of the ui-layer to delete customer.
> The _customerManager deletes customer from the list in the data-layer.
> The _currentList is the reference to the list of the data-layer.
> That means that the _currentList has one element less after that and actualy there is no need for the loop to delete the element for the second time...
>
> List<Customer> _currentList = _customerManager.GetAll();
>
> void view_DeleteCustomer(object sender, CustomerEventArgs e)
> {
> if (e.Customer == null)
> return;
> Customer deleted = e.Customer;
> deleted.Status = CustomerStatus.Deleted;
> _customerManager.Delete(deleted);
> // remove from the current list
> foreach (Customer c in _currentList)
> {
> if (c.Guid.CompareTo(deleted.Guid) == 0)
> _currentList.Remove(c);
> }
> view.UpdateCustomerList(_currentList);
> }
>
>
> and a stack... The VS also points on keyword "in" of the loop.
>
> bei System.ThrowHelper.ThrowInvalidOperationException()
> at Enumerator.MoveNext()
> bei Runtime.HakaMobile.Modules.CustomerModule.CustomersList.CustomerListPresenter.view_DeleteCustomer()
> bei Runtime.HakaMobile.Modules.CustomerModule.CustomersList.CustomerListView.DeleteCustomerMenuItem_Click()
> bei System.Windows.Forms.MenuItem.OnClick()
> bei System.Windows.Forms.Menu.ProcessMnuProc()
> bei System.Windows.Forms.Form.WnProc()
> bei System.Windows.Forms.Control._InternalWnProc()
> bei Microsoft.AGL.Forms.EVL.EnterMainLoop()
> bei System.Windows.Forms.Application.Run()
> bei Microsoft.Practices.Mobile.CompositeUI.WinForms.FormShellApplication`2.Start()
> bei Microsoft.Practices.Mobile.CompositeUI.CabApplication`1.Run()
> bei Runtime.HakaMobile.Shell.ShellApplication.Main()
>
>
>
>
>
>
>
>
>
> "<ctacke/>" <ctacke[at]opennetcf[dot]com> schrieb im Newsbeitrag news:edvO7NfXIHA.2268@TK2MSFTNGP02.phx.gbl...
> > Well you've given us pretty much no useful information to go on. My guess
> > is it's a bug in *your* iterator, not the CF.
> >
> >
> > --
> >
> > Chris Tacke, eMVP
> > Join the Embedded Developer Community
> > http://community.opennetcf.com
> >
> >
> > "c_xyTopa" <a.dratchnev@runtime.de> wrote in message
> > news:enVDz7eXIHA.1184@TK2MSFTNGP04.phx.gbl...
> >> Hi all,
> >>
> >> I get InvalidOperationException while using foreach-loop.
> >> It is not a rule, it just happens in some parts of code, in some - not.
> >> As soon as I replace the foreach-loop by for-loop no Exception is
> >> thrown...
> >>
> >> What can it be I wonder?
> >>
> >> Thanks in advance
> >>
> >
> >

Re: foreach-loop bug in CF2.0? by Jin

Jin
Wed Jan 23 23:07:17 CST 2008

On Jan 23, 6:03 pm, Simon Hart [MVP] <srhart...@yahoo.com> wrote:
> That is no suprise really after looking at your code, as you are iterating
> the collection that you are deleting from. So the MoveNext() will be at some
> point pointing to an invalid reference and not the reference that you
> intended. Basically when you call MoveNext (which is what foreach will do)
> this moves a cursor onwards, an index, then when you read the item, calling
> IEnumerable.Current the enumerator will check that cursor is not greater than
> the list of the collection, if it is, then an InvalidOperationException will
> be thrown. Because you are deleting them, this count will not be what it was
> when the iterator started. The code will look a little something like this:
>
> object IEnumerator.Current
> {
> get
> {
> if ((cursor < 0) || (cursor == mycoll.Length))
> throw new InvalidOperationException();
> return mycoll[cursor];
> }
> }
>
> As you can see, your code will break the above.
>
> Copy the items you want to delete into a separate collection maybe a
> dictionary, then remove by id or something like that, just don't do it in
> your iterator.
> --
> Simon Hart
> Visual Developer - Device Application Development MVPhttp://simonrhart.blogspot.com
>
> "c_xyTopa" wrote:
> > Sorry...
> > For example here. This is an event-handle of the ui-layer to delete customer.
> > The _customerManager deletes customer from the list in the data-layer.
> > The _currentList is the reference to the list of the data-layer.
> > That means that the _currentList has one element less after that and actualy there is no need for the loop to delete the element for the second time...
>
> > List<Customer> _currentList = _customerManager.GetAll();
>
> > void view_DeleteCustomer(object sender, CustomerEventArgs e)
> > {
> > if (e.Customer == null)
> > return;
> > Customer deleted = e.Customer;
> > deleted.Status = CustomerStatus.Deleted;
> > _customerManager.Delete(deleted);
> > // remove from the current list
> > foreach (Customer c in _currentList)
> > {
> > if (c.Guid.CompareTo(deleted.Guid) == 0)
> > _currentList.Remove(c);
> > }
> > view.UpdateCustomerList(_currentList);
> > }
>
> > and a stack... The VS also points on keyword "in" of the loop.
>
> > bei System.ThrowHelper.ThrowInvalidOperationException()
> > at Enumerator.MoveNext()
> > bei Runtime.HakaMobile.Modules.CustomerModule.CustomersList.CustomerListPresenter.view_DeleteCustomer()
> > bei Runtime.HakaMobile.Modules.CustomerModule.CustomersList.CustomerListView.DeleteCustomerMenuItem_Click()
> > bei System.Windows.Forms.MenuItem.OnClick()
> > bei System.Windows.Forms.Menu.ProcessMnuProc()
> > bei System.Windows.Forms.Form.WnProc()
> > bei System.Windows.Forms.Control._InternalWnProc()
> > bei Microsoft.AGL.Forms.EVL.EnterMainLoop()
> > bei System.Windows.Forms.Application.Run()
> > bei Microsoft.Practices.Mobile.CompositeUI.WinForms.FormShellApplication`2.Start()
> > bei Microsoft.Practices.Mobile.CompositeUI.CabApplication`1.Run()
> > bei Runtime.HakaMobile.Shell.ShellApplication.Main()
>
> > "<ctacke/>" <ctacke[at]opennetcf[dot]com> schrieb im Newsbeitragnews:edvO7NfXIHA.2268@TK2MSFTNGP02.phx.gbl...
> > > Well you've given us pretty much no useful information to go on. My guess
> > > is it's a bug in *your* iterator, not the CF.
>
> > > --
>
> > > Chris Tacke, eMVP
> > > Join the Embedded Developer Community
> > >http://community.opennetcf.com
>
> > > "c_xyTopa" <a.dratch...@runtime.de> wrote in message
> > >news:enVDz7eXIHA.1184@TK2MSFTNGP04.phx.gbl...
> > >> Hi all,
>
> > >> I get InvalidOperationException while using foreach-loop.
> > >> It is not a rule, it just happens in some parts of code, in some - not.
> > >> As soon as I replace the foreach-loop by for-loop no Exception is
> > >> thrown...
>
> > >> What can it be I wonder?
>
> > >> Thanks in advance

Another way to remove items from the list you're iteration through is
to index through them in reverse using the for-statement. Since the
iteration is heading towards index of zero (0), removing the current
index item is safe.

- Jin

Re: foreach-loop bug in CF2.0? by srhartone

srhartone
Thu Jan 24 02:26:00 CST 2008

Sure you could do that but you couldn't use the foreach keyword, instead the
for. For example:
Something like the following should work.

for (int i = mylist.Count -1; i > -1; i--)
{
Customer mycust = mylist[i];
//condition...
mylist.Remove(mycust);
}

--
Simon Hart
Visual Developer - Device Application Development MVP
http://simonrhart.blogspot.com


"Jin Chang" wrote:

> On Jan 23, 6:03 pm, Simon Hart [MVP] <srhart...@yahoo.com> wrote:
> > That is no suprise really after looking at your code, as you are iterating
> > the collection that you are deleting from. So the MoveNext() will be at some
> > point pointing to an invalid reference and not the reference that you
> > intended. Basically when you call MoveNext (which is what foreach will do)
> > this moves a cursor onwards, an index, then when you read the item, calling
> > IEnumerable.Current the enumerator will check that cursor is not greater than
> > the list of the collection, if it is, then an InvalidOperationException will
> > be thrown. Because you are deleting them, this count will not be what it was
> > when the iterator started. The code will look a little something like this:
> >
> > object IEnumerator.Current
> > {
> > get
> > {
> > if ((cursor < 0) || (cursor == mycoll.Length))
> > throw new InvalidOperationException();
> > return mycoll[cursor];
> > }
> > }
> >
> > As you can see, your code will break the above.
> >
> > Copy the items you want to delete into a separate collection maybe a
> > dictionary, then remove by id or something like that, just don't do it in
> > your iterator.
> > --
> > Simon Hart
> > Visual Developer - Device Application Development MVPhttp://simonrhart.blogspot.com
> >
> > "c_xyTopa" wrote:
> > > Sorry...
> > > For example here. This is an event-handle of the ui-layer to delete customer.
> > > The _customerManager deletes customer from the list in the data-layer.
> > > The _currentList is the reference to the list of the data-layer.
> > > That means that the _currentList has one element less after that and actualy there is no need for the loop to delete the element for the second time...
> >
> > > List<Customer> _currentList = _customerManager.GetAll();
> >
> > > void view_DeleteCustomer(object sender, CustomerEventArgs e)
> > > {
> > > if (e.Customer == null)
> > > return;
> > > Customer deleted = e.Customer;
> > > deleted.Status = CustomerStatus.Deleted;
> > > _customerManager.Delete(deleted);
> > > // remove from the current list
> > > foreach (Customer c in _currentList)
> > > {
> > > if (c.Guid.CompareTo(deleted.Guid) == 0)
> > > _currentList.Remove(c);
> > > }
> > > view.UpdateCustomerList(_currentList);
> > > }
> >
> > > and a stack... The VS also points on keyword "in" of the loop.
> >
> > > bei System.ThrowHelper.ThrowInvalidOperationException()
> > > at Enumerator.MoveNext()
> > > bei Runtime.HakaMobile.Modules.CustomerModule.CustomersList.CustomerListPresenter.view_DeleteCustomer()
> > > bei Runtime.HakaMobile.Modules.CustomerModule.CustomersList.CustomerListView.DeleteCustomerMenuItem_Click()
> > > bei System.Windows.Forms.MenuItem.OnClick()
> > > bei System.Windows.Forms.Menu.ProcessMnuProc()
> > > bei System.Windows.Forms.Form.WnProc()
> > > bei System.Windows.Forms.Control._InternalWnProc()
> > > bei Microsoft.AGL.Forms.EVL.EnterMainLoop()
> > > bei System.Windows.Forms.Application.Run()
> > > bei Microsoft.Practices.Mobile.CompositeUI.WinForms.FormShellApplication`2.Start()
> > > bei Microsoft.Practices.Mobile.CompositeUI.CabApplication`1.Run()
> > > bei Runtime.HakaMobile.Shell.ShellApplication.Main()
> >
> > > "<ctacke/>" <ctacke[at]opennetcf[dot]com> schrieb im Newsbeitragnews:edvO7NfXIHA.2268@TK2MSFTNGP02.phx.gbl...
> > > > Well you've given us pretty much no useful information to go on. My guess
> > > > is it's a bug in *your* iterator, not the CF.
> >
> > > > --
> >
> > > > Chris Tacke, eMVP
> > > > Join the Embedded Developer Community
> > > >http://community.opennetcf.com
> >
> > > > "c_xyTopa" <a.dratch...@runtime.de> wrote in message
> > > >news:enVDz7eXIHA.1184@TK2MSFTNGP04.phx.gbl...
> > > >> Hi all,
> >
> > > >> I get InvalidOperationException while using foreach-loop.
> > > >> It is not a rule, it just happens in some parts of code, in some - not.
> > > >> As soon as I replace the foreach-loop by for-loop no Exception is
> > > >> thrown...
> >
> > > >> What can it be I wonder?
> >
> > > >> Thanks in advance
>
> Another way to remove items from the list you're iteration through is
> to index through them in reverse using the for-statement. Since the
> iteration is heading towards index of zero (0), removing the current
> index item is safe.
>
> - Jin
>

Re: foreach-loop bug in CF2.0? by Jerod

Jerod
Thu Jan 24 08:51:45 CST 2008

On Jan 24, 1:26=A0am, Simon Hart [MVP] <srhart...@yahoo.com> wrote:
> Sure you could do that but you couldn't use the foreach keyword, instead t=
he
> for. For example:
> Something like the following should work.
>
> =A0for (int i =3D mylist.Count -1; i > -1; i--)
> =A0{
> =A0 =A0 =A0 Customer mycust =3D mylist[i];
> =A0 =A0 =A0//condition...
> =A0 =A0 =A0mylist.Remove(mycust);
> =A0}
>
> --
> Simon Hart
> Visual Developer - Device Application Development MVPhttp://simonrhart.blo=
gspot.com
>
>
>
> "Jin Chang" wrote:
> > On Jan 23, 6:03 pm, Simon Hart [MVP] <srhart...@yahoo.com> wrote:
> > > That is no suprise really after looking at your code, as you are itera=
ting
> > > the collection that you are deleting from. So the MoveNext() will be a=
t some
> > > point pointing to an invalid reference and not the reference that you
> > > intended. Basically when you call MoveNext (which is what foreach will=
do)
> > > this moves a cursor onwards, an index, then when you read the item, ca=
lling
> > > IEnumerable.Current the enumerator will check that cursor is not great=
er than
> > > the list of the collection, if it is, then an InvalidOperationExceptio=
n will
> > > be thrown. Because you are deleting them, this count will not be what =
it was
> > > when the iterator started. The code will look a little something like =
this:
>
> > > =A0 =A0 =A0 =A0 object IEnumerator.Current
> > > =A0 =A0 =A0 =A0 {
> > > =A0 =A0 =A0 =A0 =A0 =A0 get
> > > =A0 =A0 =A0 =A0 =A0 =A0 {
> > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 if ((cursor < 0) || (cursor =3D=3D myc=
oll.Length))
> > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 throw new InvalidOperationExce=
ption();
> > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 return mycoll[cursor];
> > > =A0 =A0 =A0 =A0 =A0 =A0 }
> > > =A0 =A0 =A0 =A0 }
>
> > > As you can see, your code will break the above.
>
> > > Copy the items you want to delete into a separate collection maybe a
> > > dictionary, then remove by id or something like that, just don't do it=
in
> > > your iterator.
> > > --
> > > Simon Hart
> > > Visual Developer - Device Application Development MVPhttp://simonrhart=
.blogspot.com
>
> > > "c_xyTopa" wrote:
> > > > Sorry...
> > > > For example here. This is an event-handle of the ui-layer to delete =
customer.
> > > > The _customerManager deletes customer from the list in the data-laye=
r.
> > > > The _currentList is the reference to the list of the data-layer.
> > > > That means that the _currentList has one element less after that and=
actualy there is no need for the loop to delete the element for the second =
time...
>
> > > > List<Customer> _currentList =3D _customerManager.GetAll();
>
> > > > void view_DeleteCustomer(object sender, CustomerEventArgs e)
> > > > {
> > > > =A0 =A0 if (e.Customer =3D=3D null)
> > > > =A0 =A0 =A0 =A0 return;
> > > > =A0 =A0 Customer deleted =3D e.Customer;
> > > > =A0 =A0 deleted.Status =3D CustomerStatus.Deleted;
> > > > =A0 =A0 _customerManager.Delete(deleted);
> > > > =A0 =A0 // remove from the current list
> > > > =A0 =A0 foreach (Customer c in _currentList)
> > > > =A0 =A0 {
> > > > =A0 =A0 =A0 =A0 if (c.Guid.CompareTo(deleted.Guid) =3D=3D 0)
> > > > =A0 =A0 =A0 =A0 =A0 =A0 _currentList.Remove(c);
> > > > =A0 =A0 }
> > > > =A0 =A0 view.UpdateCustomerList(_currentList);
> > > > }
>
> > > > and a stack... The VS also points on keyword "in" of the loop.
>
> > > > bei System.ThrowHelper.ThrowInvalidOperationException()
> > > > at Enumerator.MoveNext()
> > > > bei Runtime.HakaMobile.Modules.CustomerModule.CustomersList.Customer=
ListPresent=ADer.view_DeleteCustomer()
> > > > bei Runtime.HakaMobile.Modules.CustomerModule.CustomersList.Customer=
ListView.De=ADleteCustomerMenuItem_Click()
> > > > bei System.Windows.Forms.MenuItem.OnClick()
> > > > bei System.Windows.Forms.Menu.ProcessMnuProc()
> > > > bei System.Windows.Forms.Form.WnProc()
> > > > bei System.Windows.Forms.Control._InternalWnProc()
> > > > bei Microsoft.AGL.Forms.EVL.EnterMainLoop()
> > > > bei System.Windows.Forms.Application.Run()
> > > > bei Microsoft.Practices.Mobile.CompositeUI.WinForms.FormShellApplica=
tion`2.Star=ADt()
> > > > bei Microsoft.Practices.Mobile.CompositeUI.CabApplication`1.Run()
> > > > bei Runtime.HakaMobile.Shell.ShellApplication.Main()
>
> > > > "<ctacke/>" <ctacke[at]opennetcf[dot]com> schrieb im Newsbeitragnews=
:edvO7NfXIHA.2268@TK2MSFTNGP02.phx.gbl...
> > > > > Well you've given us pretty much no useful information to go on. =
=A0My guess
> > > > > is it's a bug in *your* iterator, not the CF.
>
> > > > > --
>
> > > > > Chris Tacke, eMVP
> > > > > Join the Embedded Developer Community
> > > > >http://community.opennetcf.com
>
> > > > > "c_xyTopa" <a.dratch...@runtime.de> wrote in message
> > > > >news:enVDz7eXIHA.1184@TK2MSFTNGP04.phx.gbl...
> > > > >> Hi all,
>
> > > > >> I get InvalidOperationException while using foreach-loop.
> > > > >> It is not a rule, it just happens in some parts of code, in some =
- not.
> > > > >> As soon as I replace the foreach-loop by for-loop no Exception is=

> > > > >> thrown...
>
> > > > >> What can it be I wonder?
>
> > > > >> Thanks in advance
>
> > Another way to remove items from the list you're iteration through is
> > to index through them in reverse using the for-statement. =A0Since the
> > iteration is heading towards index of zero (0), removing the current
> > index item is safe.
>
> > - Jin- Hide quoted text -
>
> - Show quoted text -

A way to do it without implicit looping would be to use the RemoveAll
method by creating a Predicate<Customer>. Its a cool trick that might
not be right for your solution but I thought I would post it since
there a lot of people that don't know anything about anonymous
delegates. I'm not sure of the performance impact, and if this method
is really bad I hope somebody would let me know ;)

//Your remove code would change to the following.
int numberRemoved =3D _currentList.RemoveAll( c.GeneratePredicate() );

//You would need to add a GeneratePredicate method, probably at the
class level to make it easier on yourself in case you needed to change
the comparison.
public Predicate<Customer> GeneratePredicate()
{
return delegate( Customer c )
{
return _guid =3D=3D c.Guid;
};
}