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 willdo)
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 itin
your iterator.
--
Simon Hart
Visual Developer - Device Application Development MVPhttp://simonrhart..blogspot.com
:
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 andactualy 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 NewsbeitragWell 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
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