To say of what is that it is not, or of what is not that it is, is false, while to say of what is that it is, and of what is not that it is not, is true. | Aristoteles
Verwijderd
The foreach statement is used to iterate through the collection to get the desired information, but should not be used to change the contents of the collection to avoid unpredictable side effects.
Give a man a game and he'll have fun for a day. Teach a man to make games and he'll never have fun again.
Dus ik heb het op de volgende wijze opgelost!
1
2
3
4
5
6
7
8
9
10
| ArrayList al = new ArrayList(this.Controls.Count);
foreach(Control c in this.Controls)
{
if (c.GetType() == typeof(TextBox))
al.Add(c);
}
foreach (Control c in al)
this.Controls.Remove(c); |
Andere suggesties welkom
To say of what is that it is not, or of what is not that it is, is false, while to say of what is that it is, and of what is not that it is not, is true. | Aristoteles
Maareh, je oplossing zoals deze nu is is nogal omslachtig en inefficient. Waarom loop je niet gewoon van achter naar voor door je control Collection ?
1
2
3
4
5
6
7
| for( int i = this.Controls.Count - 1; i >= 0; i-- )
{
if( ... )
{
this.Controls.Remove (Controls[i]);
}
} |
zoiets ongeveer. (Wellicht heb je ook een RemoveAt method die de index als parameter neemt).
https://fgheysels.github.io/
Give a man a game and he'll have fun for a day. Teach a man to make games and he'll never have fun again.
Had ik nog niet aan gedacht.
thnx
To say of what is that it is not, or of what is not that it is, is false, while to say of what is that it is, and of what is not that it is not, is true. | Aristoteles
Gelukkig slaat een ControlCollection zijn Controls op in een ArrayList dus dan zal het wel gewoon efficient zijn..oisyn schreef op zondag 03 december 2006 @ 19:25:
En hoe efficient denk je dat dat is als die controls container een linked list is?
“Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.”
Verwijderd
Inderdaad, daar verwonderde ik mij ook al over. Dit overkomt mij ook wel eens, maar aan de hand van de exceptie zie ik altijd dat ik het (weer eens) fout doewhoami schreef op zondag 03 december 2006 @ 18:19:
Ik vind het zowiezo vreemd dat je geen exception kreeg bij je eerste code...
De syntax is naar mijn mening correct, dus de compiler zal geen fouten aangeven! Tevens kan je in een foreach wel degelijk elementen wijzigen, als je er maar goed over na hebt gedacht!
To say of what is that it is not, or of what is not that it is, is false, while to say of what is that it is, and of what is not that it is not, is true. | Aristoteles
Normaal gezien zou je een exception moeten krijgen als je collectie wijzigt als je er met een foreach doorloopt. (En dat doe je, want je verwijderd er elementen uit).
https://fgheysels.github.io/
Maar als ik bijvoorbeeld door een array loop van objecten die ik zelf gemaakt heb. Bv. het object Parent. en daar hang ik array aan van children. Dan kan ik daar met een foreach doorheenlopen en desgewenst ongestraft toch een andere child aan hangen. Of maak ik hier een grove denkfout? Ik doe dit op deze manier nl. wel vaker
To say of what is that it is not, or of what is not that it is, is false, while to say of what is that it is, and of what is not that it is not, is true. | Aristoteles
(althans als ik je goed begrijp).
Als je dit doet:
1
2
3
4
| foreach( Parent p in myParents )
{
p.AddChild (new child());
} |
Dan wijzig je je collectie niet. Er blijven not altijd evenveel en dezelfde objecten in je collection.
Als je echter dit dot:
1
2
3
4
5
| foreach( SomeElement s in myCollection )
{
if( s.Id == 2 )
myCollection.Remove (s);
} |
Dan zal je een excpetion krijgen (en dat is ongeveer hetzelfde wat je doet in je code in je topicstart).
https://fgheysels.github.io/
Bedankt nog voor de verdere toelichting!
To say of what is that it is not, or of what is not that it is, is false, while to say of what is that it is, and of what is not that it is not, is true. | Aristoteles