Click here to Skip to main content
15,886,806 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I have an app written in c++ mfc that I moved it to c#.

in this mfc app I have many nested for loops as follows:
C#
getSeries()
{

  if (flag == pass) goto ep;

  for i1 ...
    ...
    for i2 ...
      ...
      for i3 ...
        ...
        for i4 ...
          .
          .
          .
             for ...
             {
               ...
               return;
               ep;
             }
           }
         }
     }//last for

}// end of getSeries()

this work fine in c++ mfc

but in c# won't compile because of the goto scope

Can someone help in this

Thank you for any help

What I have tried:

I haven't a difficulty to try something.
Posted
Updated 7-Oct-20 22:32pm
v2

Even in C++ such a code it is ugly and the goto possibly unjustified. Probably there are better ways to implement your algorithm, but wthout seeing the actual code, I cannot help further.
 
Share this answer
 
Comments
Maciej Los 8-Oct-20 2:50am    
5ed!
CPallini 8-Oct-20 3:01am    
Thank you, Maciej!
Basically, don't use goto at all.

If the code you have used is C++ and makes use of goto then it's quite probably badly designed - nesting 5 deep loops is a bad sign, but trying to "jump" into the middle of a loop is a poor idea and a sign that the original code was translated from an earlier language, maybe even assembler code.

C# has scope on goto labels to make code like that impossible: you can only use goto to reach a label in the same scope, or a "higher level" scope in which the current is embedded. This is by design, and it's to prevent the complications that such jumps would cause, and the spaghetti code that results from them.

What I would suggest is that you look at the original code, work out what it does and design modern C# code that doesn't require goto at all. I've been using C since the 80's, C++ since the 90's, and C# since the 00's - and never once have I had to use a goto in real code!
 
Share this answer
 
Comments
Maciej Los 8-Oct-20 2:50am    
5ed!
Member 13569650 8-Oct-20 3:10am    
your comment is known to me.
and thank you for the saying it to

also I think in rare cases like
this the compiler should help
and allow this if the developer
take its risk.
me again.

my mfc app was built in 2006
and updated in 2020 with tchar.h
all the app composite of methods and organised well.

in all my apps l have this single
goto
I tested my app many times
and its work fantastic.

l now I am in state of rearange my app methods in a way to
remove this goto.

thank you very much.
OriginalGriff 8-Oct-20 3:50am    
No, the whole idea is that you don't write code like that - not since the sixties, anyway! :laugh:
The problem is maintenance - code that jumps into loops is hard to change, hard to fix: as you have seen when you try to translate it to C#.
And the maintenance phase of development lasts orders of magnitude longer than the original "coding" phase.
If you are still using goto in modern structured, OOPs based languages like C# and C++ (heck, even in C!) then it implies a lack of understanding of what structured design and OOPs principles are all about. Just becuase you *can* do something, doesn't mean you *should*

I cannot think of any company I have worked for or done work for in the last thirty years where code like that would stand any chance of passing a code review!
Member 13569650 8-Oct-20 4:07am    
HiI don't agree with you.

In a very very rare case you must
use a goto statement like I used.

Say you want to write an application
that use an algorithm structured
in nested loops (say you haven't found non nested loops algorithm)

you have 2 options.

1 continue to search for non nested loops algorithm no matter how long
it will take.

2 use a goto . and continue to search.

I choose option 2.

note: the app need to check each
algorithm cycle and if the check
is ok we stop cycling.
and also we can't save and then
cycling because that a big cycles

thanks
OriginalGriff 8-Oct-20 4:36am    
You are allowed to disagree, but I'm pretty sure I'm on the side of the majority here! :laugh:

Let's see: I posted the code to the lounge and asked for comments:

https://www.codeproject.com/Messages/5754427/Would-this-pass-code-review-where-you-are
Is this essentially what you want happening? No goto needed.

getSeries()
{

  if (flag == pass) 
   {
     ep;
   }
else
   {
    for i1 ...
      ...
      for i2 ...
        ...
        for i3 ...
          ...
          for i4 ...
          .
          .
          .
            for ...
             {
               ...
               return;
               ep;
             }
           }
         }
     }  //didn't count braces, may not match up

}// end of getSeries()
 
Share this answer
 
Comments
Member 13569650 8-Oct-20 4:40am    
yes
but in the else option
I want to be in the most iner loop
because there's I got input
from the algorithm
Maciej Los 8-Oct-20 4:48am    
I doubt. This:
if (flag == pass) 
{
  ep;
}
else
{
...
}


can be shorten this way:
if (flag == pass) return; // ;)
Member 13569650 9-Oct-20 0:22am    
Hithe discussion here make me
forced to find a solution.

I modified some methods
and add some manipulated methods.

and put them in the most iner loop

thank you very much for making
me to be forced to reach
this solution.

thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900