By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,481 Members | 3,078 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,481 IT Pros & Developers. It's quick & easy.

Enumerating and removing

P: n/a
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?

Nov 22 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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.

"Kevin in Chicago" <Kevin in Ch*****@discussions.microsoft.com> wrote in
message news:95**********************************@microsof t.com...
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?

Nov 22 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.