Check alle échte Black Friday-deals Ook zo moe van nepaanbiedingen? Wij laten alleen échte deals zien

[.NET C#] foreach doorloopt niet alle elementen

Pagina: 1
Acties:
  • 316 views sinds 30-01-2008
  • Reageer

  • Spiral
  • Registratie: December 2005
  • Niet online
Ik heb een hele vreemde situatie en snap niet waardoor het komt:

code:
1
2
3
4
5
foreach (Control c in this.Controls)
{
    if (c.GetType() == typeof(TextBox))
        this.Controls.Remove(c);
}


Er zitten 8 elementen in this.Controls. Als ik this.Controls.Count doe geeft hij ook 8 elementen weer.
Maar met een foreach stapt hij uit de lus na het 6e element. Ik snap werkelijk waar niet waarom!

De tekstboxen worden dynamisch aangemaakt met de volgende code:

code:
1
2
3
4
5
6
7
8
9
                for (int i = 0; i < 5; i++)
                {
                    TextBox t = new TextBox();
                    t.Size = btnBlade.Size;
                    t.Location = new Point(right + 5, btnBlade.Top);
                    t.LostFocus += new EventHandler(this.TextBoxLostFocus);
                    this.Controls.Add(t);
                    right = t.Right;
                }


Het event wordt wel gewoon bij alle textboxen aangeroepen. Wat doe ik fout? Ik heb .NET 2.0 en dacht mss is het framework plat dus heb even OS gereboot maar dat mocht niet helpen.

Alvast bedankt.

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

Waarschijnlijk omdat je in je lus 2 textboxen verwijderd? Daardoor wordt de count lager en stopt hij er na 6x mee?

  • .oisyn
  • Registratie: September 2000
  • Nu online

.oisyn

Moderator Devschuur®

Demotivational Speaker

Typisch gevalletje documentatie lezen:
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.


  • Spiral
  • Registratie: December 2005
  • Niet online
Bedankt! Inderdaad dom van me Controls.Count wordt natuurlijk constant geüpdate. En tevens als verschuiven de objecten in Controls in het array.

Dus ik heb het op de volgende wijze opgelost!

code:
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 :D

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


  • whoami
  • Registratie: December 2000
  • Laatst online: 10:52
Ik vind het zowiezo vreemd dat je geen exception kreeg bij je eerste code...

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 ?
code:
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/


  • .oisyn
  • Registratie: September 2000
  • Nu online

.oisyn

Moderator Devschuur®

Demotivational Speaker

En hoe efficient denk je dat dat is als die controls container een linked list is? :)

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.


  • Spiral
  • Registratie: December 2005
  • Niet online
Prima tip!

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


  • Woy
  • Registratie: April 2000
  • Niet online

Woy

Moderator Devschuur®
.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? :)
Gelukkig slaat een ControlCollection zijn Controls op in een ArrayList dus dan zal het wel gewoon efficient zijn.

“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

whoami schreef op zondag 03 december 2006 @ 18:19:
Ik vind het zowiezo vreemd dat je geen exception kreeg bij je eerste code...
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 doe

  • Spiral
  • Registratie: December 2005
  • Niet online
Waarom zal het niet gaan compileren?

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


  • whoami
  • Registratie: December 2000
  • Laatst online: 10:52
Ik heb nergens gezegd dat het niet zou compileren, ik heb gezegd dat ik het raar vind dat je geen exception krijgt. (en dat krijg je dus at - runtime).
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/


  • Spiral
  • Registratie: December 2005
  • Niet online
Oeps! Sorry inderdaad je had het over een exception!

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 :$ Maar goed nogmaals ik doe dat alleen als ik weet dat het geen kwaad kan.

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


  • whoami
  • Registratie: December 2000
  • Laatst online: 10:52
Daar wijzig je niet je collection, maar wijzig je een object dat deel uitmaakt van je collection.
(althans als ik je goed begrijp).
Als je dit doet:
code:
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:
code:
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/


  • Spiral
  • Registratie: December 2005
  • Niet online
Helder! Ik begrijp je helemaal.
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

Pagina: 1