Jump to content
Xtreme .Net Talk

Recommended Posts

Posted
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.
Posted
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

Posted
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.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...