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. 13 14281
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.
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.
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
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.
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.
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.
"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.
"=?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
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. > >
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.
<"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
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
<"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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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)
{...
|
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.
...
|
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...
|
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...
|
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...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: linyimin |
last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
|
by: erikbower65 |
last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA:
1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
|
by: kcodez |
last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: lllomh |
last post by:
How does React native implement an English player?
|
by: Mushico |
last post by:
How to calculate date of retirement from date of birth
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
| |