HighTechTalks DotNet Forums  

Best way to remove items from a List(Of ...)

VB.net microsoft.public.dotnet.languages.vb


Discuss Best way to remove items from a List(Of ...) in the VB.net forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
BobRoyAce
 
Posts: n/a

Default Best way to remove items from a List(Of ...) - 01-11-2012 , 12:08 AM






Lets say that I have two classes, ClassA and ClassB. Further suppose
that ClassA has, as one of it's properties, a List(Of ClassB). For
example...

Public Class ClassA
Private _Property1 as integer
Private _Property2 as List(Of ClassB)

Public Property Property1 as integer
...
End Property

Public Readonly Property Property2 as List(Of ClassB)
...
End Property
End Class

Now, one of the properties in ClassB is IsToBeDeleted. What I want to
be able to do is to delete each of the ClassB items in the List(Of
ClassB) if their IsToBeDeleted property = True.

What is the best way to implement something like this?

I tried the following, but this doesn't work...

For Each oClassB As ClassB In _Property2
If (oAssociatedIso.IsToBeRemoved = True) Then
_AssociatedIsos.Remove(oAssociatedIso)
_ItemsWereRemovedFrom = True
End If
Next

Once an item is removed from the List, the collection has changes and
the Next statement raises an exception as a result.

Reply With Quote
  #2  
Old   
Andrew Morton
 
Posts: n/a

Default Re: Best way to remove items from a List(Of ...) - 01-11-2012 , 03:47 AM






BobRoyAce wrote:
Quote:
I tried the following, but this doesn't work...

For Each oClassB As ClassB In _Property2
If (oAssociatedIso.IsToBeRemoved = True) Then
_AssociatedIsos.Remove(oAssociatedIso)
_ItemsWereRemovedFrom = True
End If
Next

Once an item is removed from the List, the collection has changes and
the Next statement raises an exception as a result.
Try an ordinary For i=someList.Count-1 To 0 Step -1 loop.

--
Andrew

Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.