Exit Sub in C#

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tina

    Exit Sub in C#

    In vb.net to get out of a sub or a function we can say Exit Sub. How can I
    get out of a void method in C#. I can't find that documented anywhere.

    Thanks,
    T


  • Yury

    #2
    Re: Exit Sub in C#

    use return.

    Example:

    void SomeMethod(){
    if (condition)
    return;
    else
    DoSmth()
    }

    Comment

    • Barry Kelly

      #3
      Re: Exit Sub in C#

      "Tina" <tinamseaburn@n ospammeexcite.c om> wrote:
      [color=blue]
      > In vb.net to get out of a sub or a function we can say Exit Sub. How can I
      > get out of a void method in C#. I can't find that documented anywhere.[/color]

      return

      -- Barry

      --

      Comment

      • Jeffrey Hornby

        #4
        RE: Exit Sub in C#

        in C# you use 'return'.

        Actually, Exit Sub is deprecated in vb.net. You should be using return
        there too.

        Jeff

        --
        Jeffrey Hornby
        Hornby Consulting, Inc.



        "Tina" wrote:
        [color=blue]
        > In vb.net to get out of a sub or a function we can say Exit Sub. How can I
        > get out of a void method in C#. I can't find that documented anywhere.
        >
        > Thanks,
        > T
        >
        >
        >[/color]

        Comment

        • JimD

          #5
          Re: Exit Sub in C#

          Tina wrote:[color=blue]
          > In vb.net to get out of a sub or a function we can say Exit Sub. How can I
          > get out of a void method in C#. I can't find that documented anywhere.
          >
          > Thanks,
          > T[/color]

          return;

          Jim
          --
          =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
          There's no place like 127.0.0.1
          =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
          JimD
          Central FL, USA, Earth, Sol

          Comment

          • Mark Rae

            #6
            Re: Exit Sub in C#

            "Tina" <tinamseaburn@n ospammeexcite.c om> wrote in message
            news:u4BLAVgfGH A.2172@TK2MSFTN GP04.phx.gbl...
            [color=blue]
            > In vb.net to get out of a sub or a function we can say Exit Sub. How can
            > I get out of a void method in C#. I can't find that documented anywhere.[/color]

            You use return; same as you should be using in VB.NET. "Exit Sub", like "On
            Error..." etc, is legacy VB code and is deprecated in VB.NET


            Comment

            • Paul Cheetham

              #7
              Re: Exit Sub in C#


              As you may have gathered - return will do the trick.

              However, it is generally considered bad practice to have multiple exit
              points for a function, and can make it harder to follow. So try not to
              put any return statements in except at the end of the function.
              I have not yet come across a situation where this is not possible.

              i.e. Instead of :

              if (condition)
              return;
              else
              DoStuff()

              Use :

              if (!condition)
              DoStuff()


              Paul



              Tina wrote:[color=blue]
              > In vb.net to get out of a sub or a function we can say Exit Sub. How can I
              > get out of a void method in C#. I can't find that documented anywhere.
              >
              > Thanks,
              > T
              >
              >[/color]

              Comment

              • Larry Lard

                #8
                Re: Exit Sub in C#


                Mark Rae wrote:[color=blue]
                > "Tina" <tinamseaburn@n ospammeexcite.c om> wrote in message
                > news:u4BLAVgfGH A.2172@TK2MSFTN GP04.phx.gbl...
                >[color=green]
                > > In vb.net to get out of a sub or a function we can say Exit Sub. How can
                > > I get out of a void method in C#. I can't find that documented anywhere.[/color]
                >
                > You use return; same as you should be using in VB.NET. "Exit Sub", like "On
                > Error..." etc, is legacy VB code and is deprecated in VB.NET[/color]

                Do you have a reference for that? (about Exit; I agree about On Error)

                --
                Larry Lard
                Replies to group please

                Comment

                • Peter Kirk

                  #9
                  Re: Exit Sub in C#


                  "Paul Cheetham" <PAC.News@dsl.p ipex.com> skrev i en meddelelse
                  news:eac55HkfGH A.4080@TK2MSFTN GP03.phx.gbl...[color=blue]
                  > However, it is generally considered bad practice to have multiple exit
                  > points for a function, and can make it harder to follow. So try not to put
                  > any return statements in except at the end of the function.
                  > I have not yet come across a situation where this is not possible.
                  >
                  > i.e. Instead of :
                  >
                  > if (condition)
                  > return;
                  > else
                  > DoStuff()
                  >
                  > Use :
                  >
                  > if (!condition)
                  > DoStuff()[/color]

                  But are there not occasions where it is simpler and cleaner to have mulitple
                  returns? What about if you have to loop through a list or something, to find
                  a prticular object - if you find it you return it, if not you want to do
                  something else. Why not just have a return at the point in the loop where
                  you find the object, instead of breaking from the loop, checking if you
                  found the object and then returning it, or doing something else if you
                  didn't find it.

                  Ok, I'm not entirely sure that example is perfect, but is it true a blanket
                  "only one return" is valid?


                  Comment

                  • Paul Cheetham

                    #10
                    Re: Exit Sub in C#

                    Peter Kirk wrote:[color=blue]
                    > But are there not occasions where it is simpler and cleaner to have mulitple
                    > returns? What about if you have to loop through a list or something, to find
                    > a prticular object - if you find it you return it, if not you want to do
                    > something else. Why not just have a return at the point in the loop where
                    > you find the object, instead of breaking from the loop, checking if you
                    > found the object and then returning it, or doing something else if you
                    > didn't find it.
                    >
                    > Ok, I'm not entirely sure that example is perfect, but is it true a blanket
                    > "only one return" is valid?
                    >[/color]

                    It can look simpler and cleaner sometimes, but it makes it more
                    difficult to debug - especially if you come back to it after some length
                    of time, or somebody else has to try and do it.
                    It's perfectly valid and legal code, it just tends to be frowned on.

                    I usually declare a variable RetVal at the start of the function, as the
                    same return type as the function. I initialise it to the default return
                    value (usually that to signify the function has failed)
                    The last line is return (RetVal);

                    Using your loop example:

                    ListObj RetVal = null;

                    while (ListPos < ListCount && RetVal == null)
                    {
                    if (List[ListPos].ID == FindID)
                    RetVal = List[ListPos];
                    ListPos++;
                    }

                    return (RetVal)



                    Paul

                    Comment

                    • Mark Rae

                      #11
                      Re: Exit Sub in C#

                      "Larry Lard" <larrylard@hotm ail.com> wrote in message
                      news:1148374695 .290139.23070@3 8g2000cwa.googl egroups.com...
                      [color=blue]
                      > Mark Rae wrote:[color=green]
                      >> "Tina" <tinamseaburn@n ospammeexcite.c om> wrote in message
                      >> news:u4BLAVgfGH A.2172@TK2MSFTN GP04.phx.gbl...
                      >>[color=darkred]
                      >> > In vb.net to get out of a sub or a function we can say Exit Sub. How
                      >> > can
                      >> > I get out of a void method in C#. I can't find that documented
                      >> > anywhere.[/color]
                      >>
                      >> You use return; same as you should be using in VB.NET. "Exit Sub", like
                      >> "On
                      >> Error..." etc, is legacy VB code and is deprecated in VB.NET[/color]
                      >
                      > Do you have a reference for that? (about Exit; I agree about On Error)[/color]

                      Sorry, got myself slightly confused (I really never use VB / VB.NET these
                      days). I was aware that there was some difference to do with Return in the
                      latest version, but it seems that the GoSub reserved word has now been
                      deprecated, not the Exit reserved word - sorry for the misleading
                      information.

                      This is straight out of MSDN...

                      In a Sub or Set procedure, the Return statement is equivalent to an Exit Sub
                      or Exit Property statement, and expression must not be supplied.

                      In a Function, Get, or Operator procedure, the Return statement must include
                      expression, and expression must evaluate to a data type that is convertible
                      to the return type of the procedure. In a Function or Get procedure, you
                      also have the alternative of assigning an expression to the procedure name
                      to serve as the return value, and then executing an Exit Function or Exit
                      Property statement. In an Operator procedure, you must use Return
                      expression.

                      You can include as many Return statements as appropriate in the same
                      procedure.


                      Comment

                      • Bruce Wood

                        #12
                        Re: Exit Sub in C#

                        I tend to agree with Peter on this one. I remember the "only have a
                        single return point" from the days of "proveable correctness." I
                        thought that the "only one return" edict had since been relegated to
                        the "preferable but not necessary" file. Maybe not. Personally, I find
                        the code you wrote more difficult to understand / debug / maintain than
                        this:

                        foreach (ListObj obj in List)
                        {
                        if (obj.ID == FindID)
                        {
                        return obj;
                        }
                        }
                        return null;

                        One less variable, one less test in the loop, no chance that I'll
                        forget the increment at the end of the loop... overall I find it much,
                        much simpler.

                        But then, that may be just me. :-)

                        Comment

                        • james.curran@gmail.com

                          #13
                          Re: Exit Sub in C#

                          I'm (mostly) with Peter & Bruce on this one. My rule: If the two
                          methods are functionally equlavent (such as in Paul's first example),
                          go with the single return. If the single-return requires any extra
                          code (such as in Paul's second example), go with the multiple returns.

                          Comment

                          • Bruce Wood

                            #14
                            Re: Exit Sub in C#

                            In Paul's first example, I too consider it better form to reverse the
                            condition and avoid the additional return statement.

                            Comment

                            • Jon Skeet [C# MVP]

                              #15
                              Re: Exit Sub in C#

                              Paul Cheetham <PAC.News@dsl.p ipex.com> wrote:[color=blue]
                              > As you may have gathered - return will do the trick.
                              >
                              > However, it is generally considered bad practice to have multiple exit
                              > points for a function, and can make it harder to follow. So try not to
                              > put any return statements in except at the end of the function.
                              > I have not yet come across a situation where this is not possible.[/color]

                              Well, it's considered bad practice by some. I find it can make life a
                              *lot* simpler - you can easily tell that you're "done" with a method,
                              rather than having to then *optionally* continue. You can end up with
                              horribly tortuous methods with extra variables existing just to work
                              out whether or not we already know the answer.

                              I would suggest people should try to put the return statements wherever
                              it makes the code simplest. IMO, this is usually at the first point
                              where you know what the result will be, unless you need to do any
                              "tidying up".

                              --
                              Jon Skeet - <skeet@pobox.co m>
                              http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
                              If replying to the group, please do not mail me too

                              Comment

                              Working...