473,412 Members | 2,142 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

Can I restart a foreach loop if collection changes?

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
13 14418
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
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
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
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
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
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
"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
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
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
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
<"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
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
<"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

32
by: James Curran | last post by:
I'd like to make the following proposal for a new feature for the C# language. I have no connection with the C# team at Microsoft. I'm posting it here to gather input to refine it, in an "open...
32
by: Joe Rattz | last post by:
Hmmm, I wrote the following code. I want an array of bools and I want to intialize them to false. bool bits = new bool; foreach(bool bit in bits) { bit = false; } The compiler complains...
8
by: Brad Wood | last post by:
When I do this: foreach( Button btn in myForm.Controls ) An exception is raised when no buttons exist. I would simply expect execution to continue after the foreach loop (just as would be the...
18
by: Ken Varn | last post by:
Is there any way to reset a foreach loop to re-iterate through the collection as if it were starting from the beginning? Namely, if I delete an item out of a collection, I want to be able to reset...
5
by: David C | last post by:
This is very strange. Say I have code like this. I am simply looping through a collection object in a foreach loop. Course course = new Course(); foreach(Student s in course.Students) {...
2
by: bughunter | last post by:
This is partly 'for the record' and partly a query about whether the following is a bug somewhere in .Net (whether it be the CLR, JITter, C# compiler). This is all in the context of .Net 1.1 SP1. ...
9
by: Anthony Bouch | last post by:
Everything I know about looping structures says to be careful about expressions that need to be evaluated again and again for each test/increment of a loop. I came across this piece of code the...
7
by: bonk | last post by:
Hello I am acessing a Dictionary<TKey,TValuefrom multiple threads and often in a foreach loop. While I am within one of the foreach loops the other threads must not modify the collection itself...
8
by: =?Utf-8?B?YmJn?= | last post by:
I am getting this error when I tried to modify one field inside foreach loop. public struct myStruct { public int a; public bool b; //... } private List<myStructMyStruct = new...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.