Results 1 to 11 of 11

Thread: Exit sub and C# equivalent ?[Resolved]

  1. #1

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083

    Exit sub and C# equivalent ?[Resolved]

    We used to use it in VB like so :

    VB Code:
    1. Public Sub a()
    2.         Dim blah As String
    3.  
    4. If blah = "" Then
    5.           Exit Sub
    6.  Else
    7.   'do this
    8. End If
    9.  
    10. End Sub
    I know there are other tech I can use instead but Is there similar thing in C# ?
    Last edited by Pirate; Jul 9th, 2003 at 03:24 PM.

  2. #2
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861
    Yes, all functions in C# should return something (even if nothing)
    Code:
    void MyFunc()
    {
       //Do Something
       If(blah = "")
       {
          return;
       }
       Else
       {
          do this
       }
    }
    Being educated does not make you intelligent.

    Need a weekend getaway??? Come Visit

  3. #3
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    You can also use break.

  4. #4
    Hyperactive Member Sgt-Peppa's Avatar
    Join Date
    Mar 2003
    Location
    Munich - Germany
    Posts
    476
    break wont leave the function, just the loop or select you are in!
    Keep Smiling - even if its hard
    Frankie Says Relax, wossname Says Yeah!
    wossname:--Currently I'm wearing a gimp suit and a parachute.
    C# - Base64 Blog

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    if and else must be lowercase.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  6. #6

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by Memnoch1207
    Yes, all functions in C# should return something (even if nothing)
    Code:
    void MyFunc()
    {
       //Do Something
       If(blah = "")
       {
          return;
       }
       Else
       {
          do this
       }
    }
    Does a void return value ?? and this code is wrong in C# !!!

  7. #7

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Is there any way else than Break (which is for loops and select) ?

  8. #8
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Does a void return value ?? and this code is wrong in C# !!!
    The code isn't 'wrong', just not formatted correctly because he probably just typed it in quickly. Here, this is right:
    Code:
    void MyFunc(string blah)
    {
       if(blah = "NoProcess")
       {
          return;
       }
       else
       {
          // Do some work.
       }
    }
    Also, Void isn't a value. To verify this, try doing this:
    Object myObj = MyFunc("NoProcess");
    Last edited by hellswraith; Jul 9th, 2003 at 01:06 PM.

  9. #9

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by hellswraith
    The code isn't 'wrong', just not formatted correctly because he probably just typed it in quickly. Here, this is right:
    Code:
    void MyFunc(string blah)
    {
       if(blah = "NoProcess")
       {
          return;
       }
       else
       {
          // Do some work.
       }
    }
    Also, Void isn't a value. To verify this, try doing this:
    Object myObj = MyFunc("NoProcess");
    You missed "==" in the if statement . This is not my question though , I was asking about Exit Sub in VB , Is there similar keyword in C# ?

  10. #10
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Your right, I knew I would do something simple like that wrong...
    Anyway, the
    Exit Sub
    equivlant in C# is just
    return;

    Just that simple.

    In VB:
    Code:
    Public Sub MyMethod()
    ' Blah blah blah code
    If something = something Then
       Exit Sub
    End If
    
    ' more code.....
    End Sub
    In C#:
    Code:
    public void MyMethod()
    {
    // Blah blah blah code
    if(something == something)
    {
        return;
    }
    
    // more code.....
    }
    Last edited by hellswraith; Jul 9th, 2003 at 02:42 PM.

  11. #11

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Thanks all of you guys . It seems to be "return" what I want .

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