Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools


This is very common error when you loop collection and try to remove some item inside the loop.

If you try code like below it will fail with the following error
Collection was modified; enumeration operation may not execute

Click here to copy the following block
Dim listItem As ListItem
For Each listItem In lbxTest.Items
   If listItem.Selected = True Then
       lbxTest.Items.Remove(listItem)
   End If
Next

Fix (Use reverse loop)
To fix the problem use the reverse loop with numeric index to retive the item as shown below.

Click here to copy the following block
Dim i As Integer
For i = lbxTest.Items.Count - 1 to 0 Step -' Looping Backwards1
   If lbxTest.Items(i).Selected Then
       lbxTest.Items.Remove(i)
   End If
Next



Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.