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

Can I restart a foreach loop if collection changes?

P: n/a
How can I reset the collections within a foreach to be read as a change from
within the foreach loop then restart the foreach after collections has been
changed?

foreach(string invoice in findListBox.listBox2.Items)
{
listBox2.Items count changed, restart this foreach
}

Thanks for any help.
Trint
--
Trinity Smith
c#/vb.Net Developer
EcoQuest, Intl.
Nov 17 '05 #1
Share this Question
Share on Google+
13 Replies


P: n/a
You could use break to exit the foreach loop. Around the foreach loop you
could create a loop to restart the foreach loop. Be aware that this is a
serious thread to hang up your application. Maybe you should redesign

"TrintCSD" wrote:
How can I reset the collections within a foreach to be read as a change from
within the foreach loop then restart the foreach after collections has been
changed?

foreach(string invoice in findListBox.listBox2.Items)
{
listBox2.Items count changed, restart this foreach
}

Thanks for any help.
Trint
--
Trinity Smith
c#/vb.Net Developer
EcoQuest, Intl.

Nov 17 '05 #2

P: n/a
That doesn't really tell me anything...I know how to break from a loop. What
kind of loop do you put "around" the foreach loop?
Thanks,
Trint
--
Trinity Smith
c#/vb.Net Developer
EcoQuest, Intl.
"Marinus Holkema" wrote:
You could use break to exit the foreach loop. Around the foreach loop you
could create a loop to restart the foreach loop. Be aware that this is a
serious thread to hang up your application. Maybe you should redesign

"TrintCSD" wrote:
How can I reset the collections within a foreach to be read as a change from
within the foreach loop then restart the foreach after collections has been
changed?

foreach(string invoice in findListBox.listBox2.Items)
{
listBox2.Items count changed, restart this foreach
}

Thanks for any help.
Trint
--
Trinity Smith
c#/vb.Net Developer
EcoQuest, Intl.

Nov 17 '05 #3

P: n/a
Set a bool flag before the loop to false

Within the loop set the flag to true, when necessary, and exit the loop.

After the loop, use some mechanism to test whether you need to go back and
re-do the loop
One way is to use

do
{
// loop goes here
} while flag
Nov 17 '05 #4

P: n/a
bool contin = true;
while (contin)
{
contin = false; // very important
foreach(string invoice in findListBox.listBox2.Items)
{
if (listBox2.Items count changed)
{
contin = true;
break;
}
}
}

As Marinus said, this can be a very inefficient method. Us it only if
the count rarely changes. If the count is likely to change often, find a
different algorithm (one which doesn't involve foreach)
--
--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com

"TrintCSD" <Tr******@discussions.microsoft.com> wrote in message
news:51**********************************@microsof t.com...
That doesn't really tell me anything...I know how to break from a loop. What kind of loop do you put "around" the foreach loop?
Thanks,
Trint
--
Trinity Smith
c#/vb.Net Developer
EcoQuest, Intl.
"Marinus Holkema" wrote:
You could use break to exit the foreach loop. Around the foreach loop you could create a loop to restart the foreach loop. Be aware that this is a
serious thread to hang up your application. Maybe you should redesign

"TrintCSD" wrote:
How can I reset the collections within a foreach to be read as a change from within the foreach loop then restart the foreach after collections has been changed?

foreach(string invoice in findListBox.listBox2.Items)
{
listBox2.Items count changed, restart this foreach
}

Thanks for any help.
Trint
--
Trinity Smith
c#/vb.Net Developer
EcoQuest, Intl.

Nov 17 '05 #5

P: n/a
Hi,
foreach does not provide that feature, you could do something like

bool flag=true;
while( flag )
{
try{
foreach(string invoice in findListBox.listBox2.Items)
{
listBox2.Items count changed, restart this foreach
}
flag=false;
}catch{}
}

That should work, but I haven't tried .

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"TrintCSD" <Tr******@discussions.microsoft.com> wrote in message
news:88**********************************@microsof t.com...
How can I reset the collections within a foreach to be read as a change
from
within the foreach loop then restart the foreach after collections has
been
changed?

foreach(string invoice in findListBox.listBox2.Items)
{
listBox2.Items count changed, restart this foreach
}

Thanks for any help.
Trint
--
Trinity Smith
c#/vb.Net Developer
EcoQuest, Intl.

Nov 17 '05 #6

P: n/a
Hi,

IIRC you will get an exceptioon, that's why you have to include a try/catch
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"James Curran" <ja*********@mvps.org> wrote in message
news:Ou**************@TK2MSFTNGP09.phx.gbl...
bool contin = true;
while (contin)
{
contin = false; // very important
foreach(string invoice in findListBox.listBox2.Items)
{
if (listBox2.Items count changed)
{
contin = true;
break;
}
}
}

As Marinus said, this can be a very inefficient method. Us it only if
the count rarely changes. If the count is likely to change often, find a
different algorithm (one which doesn't involve foreach)
--
--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com

"TrintCSD" <Tr******@discussions.microsoft.com> wrote in message
news:51**********************************@microsof t.com...
That doesn't really tell me anything...I know how to break from a loop.

What
kind of loop do you put "around" the foreach loop?
Thanks,
Trint
--
Trinity Smith
c#/vb.Net Developer
EcoQuest, Intl.
"Marinus Holkema" wrote:
> You could use break to exit the foreach loop. Around the foreach loop you > could create a loop to restart the foreach loop. Be aware that this is
> a
> serious thread to hang up your application. Maybe you should redesign
>
> "TrintCSD" wrote:
>
> > How can I reset the collections within a foreach to be read as a change from > > within the foreach loop then restart the foreach after collections
> > has been > > changed?
> >
> > foreach(string invoice in findListBox.listBox2.Items)
> > {
> > listBox2.Items count changed, restart this foreach
> > }
> >
> > Thanks for any help.
> > Trint
> > --
> > Trinity Smith
> > c#/vb.Net Developer
> > EcoQuest, Intl.


Nov 17 '05 #7

P: n/a
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:OS**************@TK2MSFTNGP12.phx.gbl...
IIRC you will get an exceptioon, that's why you have to include a try/catch

In that case, I'd go for a slightly different approach.

bool contin = true;
while (contin)
{
contin = false; // very important
foreach(string invoice in findListBox.listBox2.Items)
{
if (listBox2.Items about to change)
{
contin = true;
break;
}
}
if (contin)
{
change listBox2.Items
}
}

Never use an exception as part of the normal processing.

But at this point, we have to ask --- exactly what is going on inside that
foreach()? (so we can design a proper algorithm for it.)

--
--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com

"James Curran" <ja*********@mvps.org> wrote in message
news:Ou**************@TK2MSFTNGP09.phx.gbl...
bool contin = true;
while (contin)
{
contin = false; // very important
foreach(string invoice in findListBox.listBox2.Items)
{
if (listBox2.Items count changed)
{
contin = true;
break;
}
}
}

As Marinus said, this can be a very inefficient method. Us it only if the count rarely changes. If the count is likely to change often, find a different algorithm (one which doesn't involve foreach)
--
--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com

"TrintCSD" <Tr******@discussions.microsoft.com> wrote in message
news:51**********************************@microsof t.com...
That doesn't really tell me anything...I know how to break from a loop.

What
kind of loop do you put "around" the foreach loop?
Thanks,
Trint
--
Trinity Smith
c#/vb.Net Developer
EcoQuest, Intl.
"Marinus Holkema" wrote:

> You could use break to exit the foreach loop. Around the foreach loop

you
> could create a loop to restart the foreach loop. Be aware that this is > a
> serious thread to hang up your application. Maybe you should redesign
>
> "TrintCSD" wrote:
>
> > How can I reset the collections within a foreach to be read as a

change from
> > within the foreach loop then restart the foreach after collections
> > has

been
> > changed?
> >
> > foreach(string invoice in findListBox.listBox2.Items)
> > {
> > listBox2.Items count changed, restart this foreach
> > }
> >
> > Thanks for any help.
> > Trint
> > --
> > Trinity Smith
> > c#/vb.Net Developer
> > EcoQuest, Intl.



Nov 17 '05 #8

P: n/a
mdb
"=?Utf-8?B?VHJpbnRDU0Q=?=" <Tr******@discussions.microsoft.com> wrote in
news:88**********************************@microsof t.com:
How can I reset the collections within a foreach to be read as a
change from within the foreach loop then restart the foreach after
collections has been changed?

foreach(string invoice in findListBox.listBox2.Items)
{
listBox2.Items count changed, restart this foreach
}


IF you are wanting to restart the foreach because you are adding/removing
items, and you just to avoid an exception, then you really wouldn't want to
do this. Instead, create a new ArrayList from the listBox2.Items, and
iterate over that list. That way you don't run into the problem of the
collection changing during the foreach.

ArrayList a = new ArrayList(findlistBox.listBox2.Items);
foreach(string invoice in a)
{ ... }

--
-mdb
Nov 17 '05 #9

P: n/a
Hi,

The problem is that probably the collection change in another thread, so it
may happen at anytime.

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"James Curran" <ja*********@mvps.org> wrote in message
news:er**************@TK2MSFTNGP14.phx.gbl...
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us>
wrote
in message news:OS**************@TK2MSFTNGP12.phx.gbl...
IIRC you will get an exceptioon, that's why you have to include a

try/catch

In that case, I'd go for a slightly different approach.

bool contin = true;
while (contin)
{
contin = false; // very important
foreach(string invoice in findListBox.listBox2.Items)
{
if (listBox2.Items about to change)
{
contin = true;
break;
}
}
if (contin)
{
change listBox2.Items
}
}

Never use an exception as part of the normal processing.

But at this point, we have to ask --- exactly what is going on inside that
foreach()? (so we can design a proper algorithm for it.)

--
--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com

"James Curran" <ja*********@mvps.org> wrote in message
news:Ou**************@TK2MSFTNGP09.phx.gbl...
> bool contin = true;
> while (contin)
> {
> contin = false; // very important
> foreach(string invoice in findListBox.listBox2.Items)
> {
> if (listBox2.Items count changed)
> {
> contin = true;
> break;
> }
> }
> }
>
> As Marinus said, this can be a very inefficient method. Us it only if > the count rarely changes. If the count is likely to change often, find a > different algorithm (one which doesn't involve foreach)
>
>
> --
> --
> Truth,
> James Curran
> [erstwhile VC++ MVP]
>
> Home: www.noveltheory.com Work: www.njtheater.com
> Blog: www.honestillusion.com Day Job: www.partsearch.com
>
> "TrintCSD" <Tr******@discussions.microsoft.com> wrote in message
> news:51**********************************@microsof t.com...
>> That doesn't really tell me anything...I know how to break from a
>> loop.
> What
>> kind of loop do you put "around" the foreach loop?
>> Thanks,
>> Trint
>> --
>> Trinity Smith
>> c#/vb.Net Developer
>> EcoQuest, Intl.
>>
>>
>> "Marinus Holkema" wrote:
>>
>> > You could use break to exit the foreach loop. Around the foreach
>> > loop
> you
>> > could create a loop to restart the foreach loop. Be aware that this is >> > a
>> > serious thread to hang up your application. Maybe you should
>> > redesign
>> >
>> > "TrintCSD" wrote:
>> >
>> > > How can I reset the collections within a foreach to be read as a
> change from
>> > > within the foreach loop then restart the foreach after collections
>> > > has
> been
>> > > changed?
>> > >
>> > > foreach(string invoice in findListBox.listBox2.Items)
>> > > {
>> > > listBox2.Items count changed, restart this foreach
>> > > }
>> > >
>> > > Thanks for any help.
>> > > Trint
>> > > --
>> > > Trinity Smith
>> > > c#/vb.Net Developer
>> > > EcoQuest, Intl.
>
>



Nov 17 '05 #10

P: n/a
Hi,

If you want to make it thread safe, then create it like:

private ArrayList m_array = ArrayList.Synchronized(new ArrayList());

and do:

lock(m_array.SyncRoot)
{
foreach(object obj in m_array)
{
// do your stuff here
}
}

that way, if other thread will try to use your synchronized array while
you are in the foreach loop, it will wait for the loop to finish.

Eyal.

Nov 17 '05 #11

P: n/a
<"Ignacio Machin \( .NET/ C# MVP \)" <ignacio.machin AT
dot.state.fl.us>> wrote:
The problem is that probably the collection change in another thread, so it
may happen at anytime.


The collection *shouldn't* be changing in another thread - if it is,
that's a different problem. This is a UI collection we're talking
about, which means that:

a) the enumeration should be occurring in the UI thread
b) no other thread should be changing it

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #12

P: n/a
Hi,

Then I concord with James that the OP should post the content of the
foreach.

where else the collection can be changed if not inside the foreach?
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
<"Ignacio Machin \( .NET/ C# MVP \)" <ignacio.machin AT
dot.state.fl.us>> wrote:
The problem is that probably the collection change in another thread, so
it
may happen at anytime.


The collection *shouldn't* be changing in another thread - if it is,
that's a different problem. This is a UI collection we're talking
about, which means that:

a) the enumeration should be occurring in the UI thread
b) no other thread should be changing it

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 17 '05 #13

P: n/a
<"Ignacio Machin \( .NET/ C# MVP \)" <ignacio.machin AT
dot.state.fl.us>> wrote:
Then I concord with James that the OP should post the content of the
foreach.

where else the collection can be changed if not inside the foreach?


Absolutely - it must be within the foreach, at least if the OP's
program is handling threading correctly.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.