Re: For vs. For Each
On 2004-08-12, Alvin Bruney [MVP] <> wrote:[color=blue]
> David Wrote[color=green]
>> Also, it eliminates a real ambiguity to the For Each statement, does
>> foreach iterate over the original collection, or over the entire
>> collection as it changes over time?[/color]
>
> I don't disagree with that. very good point indeed. but, the current
> approach makes it impossible to perform simple tasks inherent in UI
> programming (like removing multiselects in a listbox for instance).[/color]
Why is that difficult?
For each item as Object In New ArrayList(ListB ox1.SelectedIte ms)
ListBox1.Items. Remove(item)
Next
[color=blue]
> Where
> such simple tasks are overly complicated, i believe the design should be
> reviewed.[/color]
Well, the case where you want to iterate over the original items while
mutating the collection is always trivial, just copy the references and
enumerate the copy. I don't see why we'd need a different enumerator
for that. And the case where you want to alter the iteration based on
actions during the iteration is
a) ambiguous and generally domain-specific, so not suitable for the CLR; and
b) probably a really bad idea.
There's a deeper issue too, which I won't really get into. But IMHO
there's a tendency for .Net developers to overuse dumb collections, and
put a lot of logic into various enumerations in controller classes that
should really be handled by the collection class itself. It's hard to
avoid doing that, but I think it's a bad habit and I'm not sure I want
to see more language features that encourage it.
On 2004-08-12, Alvin Bruney [MVP] <> wrote:[color=blue]
> David Wrote[color=green]
>> Also, it eliminates a real ambiguity to the For Each statement, does
>> foreach iterate over the original collection, or over the entire
>> collection as it changes over time?[/color]
>
> I don't disagree with that. very good point indeed. but, the current
> approach makes it impossible to perform simple tasks inherent in UI
> programming (like removing multiselects in a listbox for instance).[/color]
Why is that difficult?
For each item as Object In New ArrayList(ListB ox1.SelectedIte ms)
ListBox1.Items. Remove(item)
Next
[color=blue]
> Where
> such simple tasks are overly complicated, i believe the design should be
> reviewed.[/color]
Well, the case where you want to iterate over the original items while
mutating the collection is always trivial, just copy the references and
enumerate the copy. I don't see why we'd need a different enumerator
for that. And the case where you want to alter the iteration based on
actions during the iteration is
a) ambiguous and generally domain-specific, so not suitable for the CLR; and
b) probably a really bad idea.
There's a deeper issue too, which I won't really get into. But IMHO
there's a tendency for .Net developers to overuse dumb collections, and
put a lot of logic into various enumerations in controller classes that
should really be handled by the collection class itself. It's hard to
avoid doing that, but I think it's a bad habit and I'm not sure I want
to see more language features that encourage it.
Comment