Return 陳述式 (Visual Basic)Return Statement (Visual Basic)

將控制權傳回給呼叫Function GetSub Set、、或Operator程式的程式碼。Returns control to the code that called a Function, Sub, Get, Set, or Operator procedure.


Return expression  


在、 Function Get或程式中是必要的。OperatorRequired in a Function, Get, or Operator procedure. 運算式, 表示要傳回給呼叫程式碼的值。Expression that represents the value to be returned to the calling code.


Return Exit Property expression在或程式Set 中,語句相當於或語句,而且不得提供Exit SubSubIn a Sub or Set procedure, the Return statement is equivalent to an Exit Sub or Exit Property statement, and expression must not be supplied.

expression expression在、或程式Operator中,Return語句必須包含, 而且必須評估為可轉換成程式之傳回類型的資料類型。 Get FunctionIn 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. Exit Function Exit Property在或程式Get中, 您也可以選擇將運算式指派給程式名稱, 以做為傳回值, 然後執行或語句。 FunctionIn 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. 在程式Operator中, 您必須使用Return expressionIn an Operator procedure, you must use Return expression.

在相同的程式中Return , 您可以包含多個適當的語句。You can include as many Return statements as appropriate in the same procedure.


Finally區塊中的程式碼會在遇到Return TryCatch區塊中的語句之後, 但在該Return語句執行之前執行。The code in a Finally block runs after a Return statement in a Try or Catch block is encountered, but before that Return statement executes. 語句不能包含Finally在區塊中。 ReturnA Return statement cannot be included in a Finally block.


下列範例會使用Return語句數次, 以在程式不需要執行任何動作時返回呼叫程式碼。The following example uses the Return statement several times to return to the calling code when the procedure does not have to do anything else.

Public Function GetAgePhrase(ByVal age As Integer) As String
    If age > 60 Then Return "Senior"
    If age > 40 Then Return "Middle-aged"
    If age > 20 Then Return "Adult"
    If age > 12 Then Return "Teen-aged"
    If age > 4 Then Return "School-aged"
    If age > 1 Then Return "Toddler"
    Return "Infant"
End Function

另請參閱See also