Results 1 to 5 of 5

Thread: [RESOLVED] Using exit method...

  1. #1

    Thread Starter
    Addicted Member zahadumy's Avatar
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    187

    Resolved [RESOLVED] Using exit method...

    VB Code:
    1. if condition then
    2.      ...
    3. end if
    VB Code:
    1. if not condition then
    2.      exit sub
    3. end if
    4.  
    5. ...

    Which of these 2 should we generally use? I don't know if it's just a matter of taste of using exit or not, but I like using it, although the code looks kind of messy if someone else reads it. Is there anything wrong about this?
    The first step in solving a problem is to define the problem clearly.

    -----
    Icons | EZIconConverter | Popup Messages
    101 samples: 2003 2005
    Code converter: C# -> VB .NET | VB .NET -> C# | VB .NET <-> C#


    -----
    Visual Studio 2005/.NET Framework 2.0

  2. #2
    Frenzied Member Inuyasha1782's Avatar
    Join Date
    May 2005
    Location
    California, USA
    Posts
    1,035

    Re: Using exit method...

    I hardly use "Exit Sub" in my coding. Usually only when I have large structured subs that require to exit multiple times. Generally if my program is going to have a single if statement I go like:

    VB Code:
    1. Private Sub Test()
    2.     If condition Then
    3.         ...
    4.     End If
    5. End Sub

    Then if the condition is false nothing would have happened in the first place.
    Age - 15 ::: Level - Advanced
    If you find my post useful please ::Rate It::


  3. #3

    Thread Starter
    Addicted Member zahadumy's Avatar
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    187

    Re: Using exit method...

    Yes, but I don't like to have in my code too many imbricated if's, but I agree that too many exit's look confusing when someone else read the code. In my case noone will read the code I write now, but I will be a programmer soon, so I'd like to know how things should be done...
    Thank you.
    The first step in solving a problem is to define the problem clearly.

    -----
    Icons | EZIconConverter | Popup Messages
    101 samples: 2003 2005
    Code converter: C# -> VB .NET | VB .NET -> C# | VB .NET <-> C#


    -----
    Visual Studio 2005/.NET Framework 2.0

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Using exit method...

    First off, typing Return is probably faster than typing exit sub.

    Some people STRONGLY dislike having more than one exit point for a sub or function, but I am not in that camp. I think testing for a condition at the beginning of a sub and bailing out if the condition is not met is entirely understandable. If people would have a difficult time with what amounts to an assertion, then comment the heck out of it.
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    Addicted Member zahadumy's Avatar
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    187

    Re: Using exit method...

    Quote Originally Posted by Shaggy Hiker
    First off, typing Return is probably faster than typing exit sub.
    You do have a point
    The first step in solving a problem is to define the problem clearly.

    -----
    Icons | EZIconConverter | Popup Messages
    101 samples: 2003 2005
    Code converter: C# -> VB .NET | VB .NET -> C# | VB .NET <-> C#


    -----
    Visual Studio 2005/.NET Framework 2.0

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