Enumerator failing...

froodley

Active member
Joined
Apr 20, 2010
Messages
26
Programming Experience
1-3
Hi, all,

I have a databound listbox. I was refreshing the binding, then using this code:


For Each i As Object In this_lb.Items
If CType(i, ClassA) = currentSelection Then
this_lb.SelectedItem = i
End If
Next

...to restore the current selection, but this is failing (on the assignment, selecteditem = i) if I have changed the underlying data source for the list box, even thought I am refreshing its currency manager before the enumerator:


CType(thisCtrl.BindingContext(thisCtrl.DataSource), CurrencyManager).Refresh()

It's giving me the "enumerator failed because underlying ienumerable changed" error.

If I do the same thing this way:

For i = 0 To this_lb.Items.Count - 1
If CType(this_lb.Items(i), ClassA) = currentSelection Then
this_lbTests.SelectedIndex = i
End If
Next

... it works fine in the same scenario. What's going on here? What's changing the list? There's no threading here...
 
Ah. But I assume that still involves enumerating the list somewhere on the framework side?

I'm more curious to know what's going on; the index method and your solution both work around the issue...
 
Back
Top