Compartilhar via


Instrução Return (Visual Basic)

Returns control to the code that called a Function, Sub, Get, Set, or Operator procedure.

Return
-or-
Return expression

Part

  • expression
    Required in a Function, Get, or Operator procedure. Expression that represents the value to be returned to the calling code.

Comentários

In a Sub or Set procedure, the Return statement is equivalent to an Exit Sub or Exit Property statement, and expression must not be supplied.

In 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. In 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. In an Operator procedure, you must use Return expression.

You can include as many Return statements as appropriate in the same procedure.

ObservaçãoObservação

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. In this situation, a Return statement in the Finally block executes before the initial Return statement. This gives a different return value. To prevent this potentially confusing situation, avoid using Return statements in Finally blocks.

Exemplo

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

Consulte também

Referência

Instrução Function (Visual Basic)

Instrução Sub (Visual Basic)

Declaração Get

Declaração Set (Visual Basic)

Instrução Operator

Propriedade declaração

Declaração Saída (Visual Basic)

Instrução Try...Catch...Finally (Visual Basic)