Looping
-
I am running through a list (named sublist in my code) of lines checking to see if any match where the user has clicked. At the moment I am using the following code which does work
bool test = false; int hold; int i =0; while (!test && i < sublist.Count) { test = sublist\[i\].DeleteLine(new Point(e.X, e.Y)); if (test) hold = i; i++; }
If there is a match I will delete the line from the list, so I need to know the location where the match occurred. I would like to just be able to use i, but either I get an out of bounds error or miss the last line in the array. I have looked at do/while with the same results. With for/foreach I either go through the whole list and still need to remember where the match occurred or use break which I understand is poor coding practice in a for loop. My question is, is there a way to not have to use the variable hold with a do/while or while loop?
-
I am running through a list (named sublist in my code) of lines checking to see if any match where the user has clicked. At the moment I am using the following code which does work
bool test = false; int hold; int i =0; while (!test && i < sublist.Count) { test = sublist\[i\].DeleteLine(new Point(e.X, e.Y)); if (test) hold = i; i++; }
If there is a match I will delete the line from the list, so I need to know the location where the match occurred. I would like to just be able to use i, but either I get an out of bounds error or miss the last line in the array. I have looked at do/while with the same results. With for/foreach I either go through the whole list and still need to remember where the match occurred or use break which I understand is poor coding practice in a for loop. My question is, is there a way to not have to use the variable hold with a do/while or while loop?
Try looping backwards from sublist.count to 0.
-
I am running through a list (named sublist in my code) of lines checking to see if any match where the user has clicked. At the moment I am using the following code which does work
bool test = false; int hold; int i =0; while (!test && i < sublist.Count) { test = sublist\[i\].DeleteLine(new Point(e.X, e.Y)); if (test) hold = i; i++; }
If there is a match I will delete the line from the list, so I need to know the location where the match occurred. I would like to just be able to use i, but either I get an out of bounds error or miss the last line in the array. I have looked at do/while with the same results. With for/foreach I either go through the whole list and still need to remember where the match occurred or use break which I understand is poor coding practice in a for loop. My question is, is there a way to not have to use the variable hold with a do/while or while loop?
there is nothing wrong with using break in this case, try this;
int i=0;
do
{
if (sublist[i].DeleteLine(new Point(e.X, e.Y)))
break;
} while (i++ < sublist.Count);If you are determined not to use a break you can do something like this, but I think the first example is easier to read and understand.
int i=0;
do { } while ( !sublist[i].DeleteLine(new Point(e.X, e.Y)) && (i++ < sublist.Count ));Tara
-
I am running through a list (named sublist in my code) of lines checking to see if any match where the user has clicked. At the moment I am using the following code which does work
bool test = false; int hold; int i =0; while (!test && i < sublist.Count) { test = sublist\[i\].DeleteLine(new Point(e.X, e.Y)); if (test) hold = i; i++; }
If there is a match I will delete the line from the list, so I need to know the location where the match occurred. I would like to just be able to use i, but either I get an out of bounds error or miss the last line in the array. I have looked at do/while with the same results. With for/foreach I either go through the whole list and still need to remember where the match occurred or use break which I understand is poor coding practice in a for loop. My question is, is there a way to not have to use the variable hold with a do/while or while loop?
try this:
int i=0;
do { } while ( !sublist[i].DeleteLine(new Point(e.X, e.Y)) && (i++ < sublist.Count ));actually I would prefer to use the break statement, it is easier to understand
int i = 0;
do {
if ( sublist[i].DeleteLine(new Point(e.X, e.Y) ) )
break;} while (i++ < sublist.Count);
:)
Tara
-
I am running through a list (named sublist in my code) of lines checking to see if any match where the user has clicked. At the moment I am using the following code which does work
bool test = false; int hold; int i =0; while (!test && i < sublist.Count) { test = sublist\[i\].DeleteLine(new Point(e.X, e.Y)); if (test) hold = i; i++; }
If there is a match I will delete the line from the list, so I need to know the location where the match occurred. I would like to just be able to use i, but either I get an out of bounds error or miss the last line in the array. I have looked at do/while with the same results. With for/foreach I either go through the whole list and still need to remember where the match occurred or use break which I understand is poor coding practice in a for loop. My question is, is there a way to not have to use the variable hold with a do/while or while loop?
Hi, you have an index (i), an initialisation, a continuation test, and an increment. That is called a for loop. To avoid needing the index outside the for loop, do the delete inside it, something like:
for (int i=0; i<count; i++) {
if (some test) {
do whatever needs to be done to item i
break;
}
}There is nothing wrong with break; it is intended to be used for an early termination of a loop, so it is a natural here. :)
Luc Pattyn