Removing objects from an arraylist

Broodmdh

Member
Joined
Oct 20, 2005
Messages
6
Programming Experience
1-3
I need to remove objects from an arraylist based upon certain user-specified conditions. I can successfully loop through the arraylist and remove items, but I am having a problem correctly referencing WHICH item in the arraylist I want removed. This is due my use of structures (it's an arraylist of structures). I am not sure how to reference the components of the structure within the arraylist directly (the conditions are checked against components of the structure). I'll post my code below. Any helpful suggestions are welcome.

VB.NET:
If chkName.CheckState = CheckState.Checked Then
   i = 0
   While i < tmpList.Count
      If Not aData.aName = txtName.Text Then  'The problem lies here (aData isn't advanced from one to the next within the arraylist)
         tmpList.RemoveAt(i)
      Else
         i += 1
      End If
   End While
End If
 

Broodmdh

Member
Joined
Oct 20, 2005
Messages
6
Programming Experience
1-3
Declarations

In a module:

VB.NET:
<Serializable()> Structure AssetInfo
   Public aID As Integer
   Public aName As String
   Public aInfo As String
   .......
End Structure
 
Public aData As AssetInfo[SIZE=2]
[/SIZE][SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2] aList [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] ArrayList    ' Default list - Used for another purpose
[/SIZE]Public tmpList As ArrayList

In the form_load:
VB.NET:
 [SIZE=2]
aList = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] ArrayList()
[/SIZE]tmpList = New ArrayList()
tmpList = aList    ' tmpList is used to store search results (trimmed version of aList)

Need anything else?
 

sevenhalo

Well-known member
Joined
Oct 21, 2005
Messages
137
Location
Iowa
Programming Experience
3-5
VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] tmpHold [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] AssetInfo[/SIZE]
[SIZE=2]
[/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][SIZE=2] tmpHold [/SIZE][SIZE=2][COLOR=#0000ff]In[/COLOR][/SIZE][SIZE=2] aList
[/SIZE][SIZE=2][COLOR=#0000ff]  If[/COLOR][/SIZE][SIZE=2] tmpHold.aName.Trim = txtName.trim.Text [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]    aList.RemoveAt(aList.IndexOf(tmpHold))
[/SIZE][SIZE=2][COLOR=#0000ff]  End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Next[/COLOR][/SIZE][SIZE=2] tmpHold
[/SIZE]

Will something like this work for you? I went on a limb and assumed that tmpList was created as a temporary holding conainer while you did your loop. If it's not, just use tmpList instead of aList.

Edit: I didn't include the CheckBox validation, sorry. :(
Edit (Again): Syntax change, "RemoveAt" added
 
Last edited:

Broodmdh

Member
Joined
Oct 20, 2005
Messages
6
Programming Experience
1-3
I'm probably being a little dense, but I'm having trouble throwing it all together. I can get the index of the matching structure using your code. Should I then run through the arraylist using a while loop, and removing all entries except the one(s) that match the numbers retrieved in the for loop? Does that sound right?
 

Broodmdh

Member
Joined
Oct 20, 2005
Messages
6
Programming Experience
1-3
I gave this a try, and I get the following error:

Collection was modified; enumeration operation may not execute.

I have read that you cannot move through an arraylist and remove items using a for next loop, because the index changes when a removal is done (or something to that effect). That's why I was trying to use the while loop. Do you know a way around this?
 

sevenhalo

Well-known member
Joined
Oct 21, 2005
Messages
137
Location
Iowa
Programming Experience
3-5
Instead of removing, you can place the tmpHold into your tmpList, after it's done with the for/next loop (damn microsoft and their inflexibility to decrement indexes), you'll have a full array of which records need to be removed (tmpList). Do a for/next loop based on tmpList and remove the items from aList instead.

Edit: Its kind of a mixed blessing. You have to do an extra loop, but now that you have a list of the records removed; you can implement an "Undo" :)
 

Paszt

Staff member
Joined
Jun 3, 2004
Messages
1,500
Location
Raleigh, NC - USA
Programming Experience
Beginner
FYI: When removing items in a collection using the index you can loop thru the collection in reverse (For i = aList.Count -1 to 0 step -1).
 
Top Bottom