The loops and conditional statements available in Visual Basic let you reroute your code based on data. The language includes a few other statements that let you control the action in a more direct manner.
The GoTo Statement
The GoTo statement lets you jump immediately to some other location within the current procedure. The destination of a jump is always a line label, a named line position in the current procedure. All line labels appear at the start of a logical line, and end with a colon.
PromptUser: GetValuesFromUser(numerator, denominator) If (denominator = 0) Then GoTo PromptUser quotient = numerator / denominator
In this sample, the GoTo statement jumps back to the PromptUser label when the code detects invalid data. Processing continues with the line immediately following the PromptUser label. You can't use the same label name twice in the same procedure, although you can reuse label names in different procedures. If you want, include another logic statement on the same line as your label, right after the colon, although your code will be somewhat easier to read if you keep labels on their own lines.
LabelAlone: MsgBox("It's all alone.") LabelAndCode: MsgBox("Together again.")
It's all right to include as many labels in your code as you need, but the GoTo statement is one of those elements of Visual Basic that is monitored closely by pesky international software agencies, such as the International Committee to Keep GoTo Always Gone (ICK-GAG). That group also scans computer books looking for derogatory references to their organization name; not that they would find anything like that in this book. But their core issue is that overuse of GoTo statements can lead to spaghetti code, such as the following.
Dim importantMessage As String = "Do" GoTo Step2 Step6: importantMessage &= "AG!" GoTo Step7 Step3: importantMessage &= "wit" GoTo Step4 Step2: importantMessage &= "wn " GoTo Step3 Step5: importantMessage &= "CK-G" GoTo Step6 Step4: importantMessage &= "h I" GoTo Step5 Step7: MsgBox(importantMessage)
Some people say that such code is hard to read. Others call it job security. No matter what you call it, it does make code very hard to maintain and review. You should probably keep an eye on your use of GoTo statements; if you don't, someone else might.
There are some limits placed on the use of GoTo by Visual Basic itself. You cannot jump into or out of certain multi-line statements that would result in improperly initialized code or data values. For instance, you cannot jump into the middle of a For . . . Next statement from outside of the statement, because the loop counter variable and the starting and ending ranges would not be properly initialized.
' ----- This GoTo statement will fail. GoTo InsideTheLoop For counter = 1 to 10 InsideTheLoop: MsgBox("Loop number: " & counter) Next counter
However, once you are inside of the loop, you can jump to line labels that also appear in the loop, and it's acceptable to jump out of the loop using GoTo. Some other multi-line structures impose similar restrictions.
The Return Statement
Not only can you jump around within a procedure using GoTo, you can jump right out of a procedure anytime you want using the Return statement. Normally, a procedure exits when processing reaches the last line of code in the procedure; processing then continues with the code that called the procedure. The Return statement provides a way to exit the procedure before reaching the end.
In subroutines, the Return statement appears by itself as a standalone statement.
In functions, the statement must include the value to be returned to the calling code: a variable, a literal, or an expression that must match the specified return value data type of the function.
Pre-.NET releases of Visual Basic used an Exit statement to immediately leave a procedure. These are still supported in .NET. There are three variations.
When exiting from a function, the Exit Function statement does not include a way to specify a return value. You must set the return value separately by assigning the return value to the name of the function.
Function SafeDivide(ByVal numerator As Double, _ ByVal denominator As Double) As Double ' ----- The "#" sign makes a number a Double. If (denominator = 0#) Then ' ----- Return 0 on invalid division. SafeDivide = 0# Exit Function End If Return numerator / denominator End Function
The End and Stop Statements
The End and Stop bring an immediate halt to your Visual Basic application. The End statement exits your program immediately, aborting all further code and data processing (although certain acquired resources are cleaned up).
The Stop statement suspends processing only when you are running your application within a debugger, such as the Visual Studio development environment. Stop returns control to the environment, allowing the developer to examine and possibly alter data and code before continuing on with the program. If a Stop is encountered in a standalone application running outside of the debugger, it prompts the user to debug the application using any debugger installed on the workstation. Needless to say, the user will not be amused.