+ 2

Can i use "goto" in this case, or should i use it at all?

Hey guys, I need some help or rather some with sense driven opinion, its about a code-snippet i wrote for a windows forms project and specificly about the use of "goto". I discussed it with a friend who is part of the reason i bringing the case here, because he said you should never use it. I asked my teacher about it ans even he wasnt 100% whats the best case here. So i researched a little bit and made up my mind with a "solution" i can live with ^^. But i want to here what your thoughts in this are because i had  alwyas great conversations here, here is the code: for (int col = 0; col < sideLength; col++)             {                 for (int row = 0; row < sideLength; row++)                 {                     if (pictureBoxGrid[col,row].Bounds.Contains(centerObjectPoint))                     {                         pictureboxIndex[0] = col;                         pictureboxIndex[1] = row;                         goto SUCESS;                     }                 }             }             SUCESS:

13th Jul 2018, 7:40 PM
IPhoenix IMarco
IPhoenix IMarco - avatar
8 Respuestas
0
Marcus Otterström Why would you advise against flag variables, i Check if a point is on one of the pictureboxes and if i Save the index of that picturebox and dont know if linq makes sense here on the Information i know or am i wrong here?
14th Jul 2018, 12:52 PM
IPhoenix IMarco
IPhoenix IMarco - avatar
+ 3
A lot of companies forbid goto statements so using a different method might help you in you future working career. I'd do this: bool exit = false; for (int col = 0; col < sideLength && !exit; col++) {          for (int row = 0; row < sideLength && !exit; row++)          {                     if (pictureBoxGrid[col,row].Bounds.Contains(centerObjectPoint))                     {                         pictureboxIndex[0] = col;                         pictureboxIndex[1] = row;                         exit = true;                     }          } }
13th Jul 2018, 10:54 PM
John Wells
John Wells - avatar
+ 1
yes goto is usable when trying to break out of nested loops, but other than that, i don't advise it
13th Jul 2018, 10:07 PM
hinanawi
hinanawi - avatar
+ 1
John Wells thats exactly how i changed just the variable is still called sucess ^^ i think personally thats the best solution, because everyone can understand it and it is not "complex". nice to know that companies do that. I wouldnt use goto again because it is confuses more than it solves and it is not really known anymore
14th Jul 2018, 5:07 AM
IPhoenix IMarco
IPhoenix IMarco - avatar
+ 1
hinanawi thats what i thought first to, it is really useful but not necessary and the "negatives" just outrun the use of it
14th Jul 2018, 5:16 AM
IPhoenix IMarco
IPhoenix IMarco - avatar
0
Marcus Otterström how would you change the Code?
13th Jul 2018, 7:53 PM
IPhoenix IMarco
IPhoenix IMarco - avatar
0
would only go out of the first for thats Why i used it
13th Jul 2018, 8:00 PM
IPhoenix IMarco
IPhoenix IMarco - avatar
0
Marcus Otterström its just little difference and a personal opinion of mine but i like the Version of John Wells more but thanks for your ways, to seperated it in to a method is a really nice idea and i would do that probally in the future too, whats your opinion on this way?
14th Jul 2018, 10:53 AM
IPhoenix IMarco
IPhoenix IMarco - avatar