PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
[RESOLVED] Stopping code execution-VBForums
Results 1 to 5 of 5

Thread: [RESOLVED] Stopping code execution

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2006
    Posts
    116

    Resolved [RESOLVED] Stopping code execution

    I am working on a program that the must enter specific data information. If that data is not entered I want to stop all code execution, BUT NOT THE PROGRAM.

    I had want I wanted working, but just recently added a second command button that executes the same code. So I moved the code in question to a called sub. Until I moved the code I was using exit sub, so now when the exit sub command executes it continues and runs the next called procedure. I do not want this.

    I tried Stop, but that is like a debug stop point and not want I want.
    So basically I am asking are there alternatives to exit sub?

    Am I going to have to write my own "stop" code?

  2. #2
    Hyperactive Member Frabulator's Avatar
    Join Date
    Jan 2015
    Posts
    302

    Re: Stopping code execution

    Could you set a boolean that you check periodically, and then nest it inside of the running sub and the main sub?

    VB.NET Code:
    1. If CheckForStop = True Then Exit Sub
    Oops, There it goes. Yep... my brain stopped...
    _________________________________

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Aug 2006
    Posts
    116

    Re: Stopping code execution

    Quote Originally Posted by Frabulator View Post
    Could you set a boolean that you check periodically, and then nest it inside of the running sub and the main sub?

    VB.NET Code:
    1. If CheckForStop = True Then Exit Sub
    For clarification, from what you are saying there is no statement to do what I want.

  4. #4
    Fanatic Member
    Join Date
    Nov 2017
    Posts
    931

    Re: Stopping code execution

    As posed, your question is somewhat nonsensical. If the user has entered invalid data, you don't need to stop your subroutine code, you just need to not call your subroutine code in the first place.

    An oversimplified example. Imagine I've asked the user to enter a positive number that I've stored in the variable "a", and I have code in a subroutine that I want to call that does some processing with that number.

    Code:
    'Button click code
    If a > 0 Then
      PerformCalculation(a)
    End If
    That's it. You don't see any "stop" code, because there doesn't need to be. If the user enters a negative number, the Calculation subroutine isn't called. The way to "stop" the subroutine from executing is to simply not start it when the input isn't valid.

    That being said, because you didn't post any of your code, we can't give you specific guidance on how to make your own code work.

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    34,701

    Re: Stopping code execution

    Return is probably a bit better than Exit Sub, though it really makes no difference that I'm aware of.

    Still, I agree with OB1 in that what you're asking doesn't make sense the way it is asked. We'd probably have to see the code, but I interpret what you are doing as having a series of steps, one of which is calling a Sub. If I understand you right, under certain circumstances, you want not just the Sub to quit, but whatever called the Sub should also quit. If that's the case, then rather than a Sub, use a Function, and return a Boolean from it.

    Inside the Sub, where you had Exit Sub, put Return False, but also be sure to Return True at the end of the function, or it will return False by default. Then, where you call the function, it would look like this:
    Code:
    If YourFunctionhere() = False Then
     Return
    Else
     'The code to keep on going can go here.
    End If
    My usual boring signature: Nothing

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width