Hi:
I have a wierd error that I can't seem to find out why its happening.
Problem: I am interating through a collection of combo box items. Periodically, I get the following error:
"Collection was modified; enumeration operation may not execute."
The wierd part is that I am not modifying the list (i.e. adding, removing, etc.)...or am I?
The code in question:
Dim Ctr as Integer = 0
For Each ForLpPharm As Pharmacy In cmboDefaultPharmacy.Items
If ForLpPharm.PharDBId = pMaData.PharmDbId Then
cmboDefaultPharmacy.SelectedIndex = Ctr
PharmFound = True
Exit For
End If
Ctr += 1
'Error happens here...before second interation
Next
What happens? Well, I know that the code will execute correctly once and "Ctr" will increment once to 1. The error happens after that. I also know that there are 5 items in cmboDefaultPharmacy.Items. How? Well, in an expanded version of the method with line numbers and other error trappings, I get this in the exception thrown when the error occurs.
I am unable to replicate the error in my IDE, so I can't determine the "state" of the combo box.
Thoughts?
Eric
I have a wierd error that I can't seem to find out why its happening.
Problem: I am interating through a collection of combo box items. Periodically, I get the following error:
"Collection was modified; enumeration operation may not execute."
The wierd part is that I am not modifying the list (i.e. adding, removing, etc.)...or am I?
The code in question:
Dim Ctr as Integer = 0
For Each ForLpPharm As Pharmacy In cmboDefaultPharmacy.Items
If ForLpPharm.PharDBId = pMaData.PharmDbId Then
cmboDefaultPharmacy.SelectedIndex = Ctr
PharmFound = True
Exit For
End If
Ctr += 1
'Error happens here...before second interation
Next
What happens? Well, I know that the code will execute correctly once and "Ctr" will increment once to 1. The error happens after that. I also know that there are 5 items in cmboDefaultPharmacy.Items. How? Well, in an expanded version of the method with line numbers and other error trappings, I get this in the exception thrown when the error occurs.
I am unable to replicate the error in my IDE, so I can't determine the "state" of the combo box.
Thoughts?
Eric