473,730 Members | 3,726 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.lis tBox2.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 14487
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.lis tBox2.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.lis tBox2.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.lis tBox2.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******@discu ssions.microsof t.com> wrote in message
news:51******** *************** ***********@mic rosoft.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.lis tBox2.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.lis tBox2.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******@discu ssions.microsof t.com> wrote in message
news:88******** *************** ***********@mic rosoft.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.lis tBox2.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*********@mv ps.org> wrote in message
news:Ou******** ******@TK2MSFTN GP09.phx.gbl...
bool contin = true;
while (contin)
{
contin = false; // very important
foreach(string invoice in findListBox.lis tBox2.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******@discu ssions.microsof t.com> wrote in message
news:51******** *************** ***********@mic rosoft.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.lis tBox2.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.mach in AT dot.state.fl.us > wrote
in message news:OS******** ******@TK2MSFTN GP12.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.lis tBox2.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*********@mv ps.org> wrote in message
news:Ou******** ******@TK2MSFTN GP09.phx.gbl...
bool contin = true;
while (contin)
{
contin = false; // very important
foreach(string invoice in findListBox.lis tBox2.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******@discu ssions.microsof t.com> wrote in message
news:51******** *************** ***********@mic rosoft.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.lis tBox2.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******@discu ssions.microsof t.com> wrote in
news:88******** *************** ***********@mic rosoft.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.lis tBox2.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(findl istBox.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*********@mv ps.org> wrote in message
news:er******** ******@TK2MSFTN GP14.phx.gbl...
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us >
wrote
in message news:OS******** ******@TK2MSFTN GP12.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.lis tBox2.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*********@mv ps.org> wrote in message
news:Ou******** ******@TK2MSFTN GP09.phx.gbl...
> bool contin = true;
> while (contin)
> {
> contin = false; // very important
> foreach(string invoice in findListBox.lis tBox2.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******@discu ssions.microsof t.com> wrote in message
> news:51******** *************** ***********@mic rosoft.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.lis tBox2.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

32
4149
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 Source" manner, and in an attempt to build a ground-swell of support to convince the folks at Microsoft to add it. Proposal: "first:" "last:" sections in a "foreach" block The problem: The foreach statement allows iterating over all the...
32
6516
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 on the "bit = false;" stating that bit is read-only.
8
2109
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 case with a regular for loop when the second expression is false when the loop begins). Is there an explanation for this?
18
6641
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 the loop. -- ----------------------------------- Ken Varn Senior Software Engineer Diebold Inc.
5
2653
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) { Console.WriteLine(s.StudentID); }
2
3195
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. Recently we (my fellow team members & I) observed an InvalidOperationException - 'collection has been modified', however it wasn't immediately obvious why this would be occuring. There are no modifications occuring within the loop and there is...
9
7751
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 other day and stopped to think for a minute about whether it was right or not. foreach (XmlNode node in doc.SelectNodes("/root/map/");) { //Do something }
7
6138
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 since that would cause an exception in the foreach loop "foreach can not continue because the colelction was modifed". Now what is the least expensive and threadsafe way to make sure that no other threads modifies that collection. Since one of the...
8
17067
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 List<myStruct>();
0
8943
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8770
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9229
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9176
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8184
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6725
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6030
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
2713
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2175
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.