Enumerating and removing

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello all,

I have written the following code that both enumerates and also removes from
the collection being enumerated. As the code illustrates, this causes me to
have to reset the enumerator. Clearly this isn't very attractive.

Dim enumerator As IEnumerator = _containmentTree.GetEnumerator
While (enumerator.MoveNext)
Dim classProxy As G2ProxyBase = CType(enumerator.Current, G2ProxyBase)
Dim proxyName As String = classProxy.Name
If Not _topLevelAssets.Contains(proxyName) Then
_containmentTree.Remove(classProxy)
enumerator = _containmentTree.GetEnumerator
End If
End While

I'm sure there is a better way, but I haven't thought of it yet. What do you
others think?
 
Use of "for" loop and start at the highest element. Move downward as you
go. Then just remove what your index is at. Then just continue downward.
No messing with enumerators.
 
Back
Top