epic69 Posted March 21, 2005 Posted March 21, 2005 I need to remove an object from an arraylist within a for each loop. every time i remove the object i dont need from teh array list, when it hits the next statement it exits out and says referenced object has a value of nothing. So is this possible? Thanks for your help. Quote
Tygur Posted March 21, 2005 Posted March 21, 2005 I need to remove an object from an arraylist within a for each loop. every time i remove the object i dont need from teh array list' date=' when it hits the next statement it exits out and says referenced object has a value of nothing. So is this possible? Thanks for your help.[/quote'] You have to move backwards by index: 'create the ArrayList Dim AL As New ArrayList() 'throw some numbers in it AL.Add(1) : AL.Add(2) : AL.Add(3) : AL.Add(4) : AL.Add(5) 'this For loop will remove all even numbers Dim I As Integer For I = AL.Count - 1 To 0 Step -1 If AL(I) Mod 2 = 0 Then AL.RemoveAt(I) End If Next Quote
epic69 Posted March 21, 2005 Author Posted March 21, 2005 Thanks yes i thought of doing that also, but i opted to have use a temparraylist and fill it with the objects that need to be removed. then i ran through each and removed each item from my original arraylist. its working fine now. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.