CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Jan 2010
    Posts
    1

    C# good alternatives to C++/goto statement



    Hi, I have a sample program structure in C++ as shown in the image link above. Anybody could suggest me a way to rewrite that structure into an equivalent in C# ?

    As you can see from the picture, the C++ code use "goto" statement to jump around but I would not want to do the same in C#, I would want to avoid those "goto"s.

    In fact:

    1. I can't simply rewrite using the same "goto" as in C#, goto has more limited scope than in C++. It is not allowed to jump from a code block into another block in C# like we normally do in C++. in C#, label has its scope only in the code block that it's declared.

    2. I find it hard to use switch or probably try/catch/finally since the original code uses goto to jump from outter code block to inner code block or vice versa. All those variables declared in the outter code block will not be visible to the inner one so the compiler will complain.

    Anybody encounter this before, could suggest me a best practice to archive the same thing in C# ?

    The worse case is that I may need to rewrite duplicate code to avoid "goto" ?

    Thanks and pardon my English. English is not my native language.

    Alex To

  2. #2
    Join Date
    Nov 2002
    Location
    .NET 3.5 VS2008
    Posts
    1,039

    Re: C# good alternatives to C++/goto statement

    Hi Alex To,

    Your English is very good. I didn't see any image link. YOu may need to edit your post and sort that out. I think this is an opportunity to re-write the code in the C# style (Many would say that the over-use of the 'goto' is not good C++ either). You're not copying the code think of it as re-writing it but with the benefit of something to compare against or aim at. I would even say just forget about the 'goto' and any possible equivalents in C#. You should not need to duplicate code to avoid using the 'goto'. I've said all this without looking at any of the C++ code though...But I'm convinced you can re-write it in C# without the 'goto'.

  3. #3
    Join Date
    Nov 2007
    Location
    .NET 3.5 / VS2008 Developer
    Posts
    624

    Re: C# good alternatives to C++/goto statement

    Quote Originally Posted by nelo View Post
    Hi Alex To,

    Your English is very good. I didn't see any image link. YOu may need to edit your post and sort that out. I think this is an opportunity to re-write the code in the C# style (Many would say that the over-use of the 'goto' is not good C++ either). You're not copying the code think of it as re-writing it but with the benefit of something to compare against or aim at. I would even say just forget about the 'goto' and any possible equivalents in C#. You should not need to duplicate code to avoid using the 'goto'. I've said all this without looking at any of the C++ code though...But I'm convinced you can re-write it in C# without the 'goto'.
    the link to the image is at the top of the post, before he even starts his post.

    As for alternatives, most use "while" or "do/while" loops. To exit a for loop, you can use the "break" keyword.
    ===============================
    My Blog

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: C# good alternatives to C++/goto statement

    Quote Originally Posted by nelo View Post
    I've said all this without looking at any of the C++ code though...But I'm convinced you can re-write it in C# without the 'goto'.
    You can also rewrite it in C++ without the goto as well.

  5. #5
    Join Date
    Jun 2008
    Posts
    2,477

    Re: C# good alternatives to C++/goto statement

    goto is only useful in some very specific situations. It looks to me as if your code just needs a few if statements inside of the inner loop and a 'break;' at the end of each one. Just remember that 'break' only returns you to the next block, one alone will not exit all three loops.

  6. #6
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    Re: C# good alternatives to C++/goto statement

    Consider using 'break' and 'continue' statements in both C# and C++. However, make it very clear what is happening. If the loops contain a lot of code and you are using 'break' and 'continue' statements, consider moving some of the code to a seperate function, so the meaning of the 'break' and 'continue' statements is obvious.

    Goto is a last resort. The last time I used Goto was in Commodore PET BASIC in 1982!
    Rob
    -
    Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: C# good alternatives to C++/goto statement

    Quote Originally Posted by rliq View Post
    The last time I used Goto was in Commodore PET BASIC in 1982!
    I know this sounds a bit snooty, but I agree. I haven't run into a case in C++ or C# where a goto was necessary.

  8. #8
    Join Date
    May 2009
    Location
    Bengaluru, India
    Posts
    460

    Re: C# good alternatives to C++/goto statement

    using while loops along with continue and break statements would solve your problem i suppose..

  9. #9
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: C# good alternatives to C++/goto statement

    I can back this. If I don't count Basic on my first Atari, I've used goto only once - and it survives in the code for about 30 minutes I cannot remebeer a situation where goto had been a "silver bullet". I can imagine it is useful in procedural programing, but in OOP I considere it obsolete. Despite that, If you need it, it still there with us.
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  10. #10
    Join Date
    Jan 2009
    Posts
    12

    Re: C# good alternatives to C++/goto statement

    I agree with the previous posts; I have not needed to use goto in well over a decade of programming (not since BASIC on DOS).
    You could use break/continue as the pp's suggested or split code out into logical functions and call the functions in your conditional statements.

  11. #11
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    Re: C# good alternatives to C++/goto statement

    I can back this. If I don't count Basic on my first Atari
    Wow! Seems like we need a PET/Atari forum on here. Those were the days, when block graphics was state of the art and your application either went Beep or it didn't go Beep
    Rob
    -
    Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......

  12. #12
    Join Date
    Dec 2009
    Posts
    22

    Re: C# good alternatives to C++/goto statement

    continue:

    break:
    Last edited by jonlist; February 5th, 2010 at 11:53 PM. Reason: mixed up

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured