Resolução de sobrecarga (Visual Basic)

When the Visual Basic compiler encounters a call to a procedure that is defined in several overloaded versions, the compiler must decide which of the overloads to call. It does this by performing the following steps:

  1. Acessibilidade. It eliminates any overload with an access level that prevents the calling code from calling it.

  2. Number of Parameters. It eliminates any overload that defines a different number of parameters than are supplied in the call.

  3. Tipos de dados de parâmetro. O compilador dá preferência de métodos de instância sobre os métodos de extensão . Se qualquer método de instância for encontrado, que requer a somente conversões para atender a chamada de procedimento de expansão, todos os métodos de extensão são descartados e o compilador continua com apenas os candidatos de método de instância. Se nenhuma instância método for encontrado, ele continua com métodos de instância e a extensão .

    Nesta etapa, ele elimina qualquer sobrecarga para os quais os tipos de dados dos argumentos de chamada não podem ser convertidos para os tipos de parâmetro definidos na sobrecarga.

  4. Conversões explicitas de restrição. It eliminates any overload that requires a narrowing conversion from the calling argument types to the defined parameter types. This is true whether the type checking switch (Opção declaração estrito) is On or Off.

  5. Least Widening. O compilador considera as sobrecargas restantes em pares. For each pair, it compares the data types of the defined parameters. If the types in one of the overloads all widen to the corresponding types in the other, the compiler eliminates the latter. Ou seja, ele retém a sobrecarga que requer a menor quantidade de ampliação.

  6. Single Candidate. It continues considering overloads in pairs until only one overload remains, and it resolves the call to that overload. If the compiler cannot reduce the overloads to a single candidate, it generates an error.

The following illustration shows the process that determines which of a set of overloaded versions to call.

Resolving among overloaded versions

Diagrama de fluxo de processo de resolução de sobrecarga

The following example illustrates this overload resolution process.

Overloads Sub z(ByVal x As Byte, ByVal y As Double)
End Sub
Overloads Sub z(ByVal x As Short, ByVal y As Single)
End Sub
Overloads Sub z(ByVal x As Integer, ByVal y As Single)
End Sub
Dim r, s As Short
Call z(r, s)
Dim p As Byte, q As Short
' The following statement causes an overload resolution error.
Call z(p, q)

In the first call, the compiler eliminates the first overload because the type of the first argument (Short) narrows to the type of the corresponding parameter (Byte). It then eliminates the third overload because each argument type in the second overload (Short and Single) widens to the corresponding type in the third overload (Integer and Single). The second overload requires less widening, so the compiler uses it for the call.

In the second call, the compiler cannot eliminate any of the overloads on the basis of narrowing. It eliminates the third overload for the same reason as in the first call, because it can call the second overload with less widening of the argument types. However, the compiler cannot resolve between the first and second overloads. Each has one defined parameter type that widens to the corresponding type in the other (Byte to Short, but Single to Double). The compiler therefore generates an overload resolution error.

Overloaded Optional and ParamArray Arguments

If two overloads of a procedure have identical signatures except that the last parameter is declared Opcional (Visual Basic) in one and ParamArray (Visual Basic) in the other, the compiler resolves a call to that procedure as follows:

If the call supplies the last argument as

The compiler resolves the call to the overload declaring the last argument as

No value (argument omitted)

Optional

A single value

Optional

Two or more values in a comma-separated list

ParamArray

An array of any length (including an empty array)

ParamArray

Consulte também

Tarefas

Solucionando problemas de procedimentos (Visual Basic)

Como: Definir várias versões de um procedimento (Visual Basic)

Como: Chamar um procedimento sobrecarregado (Visual Basic)

Como: Sobrecarregar um procedimento que recebe parâmetros opcionais (Visual Basic)

Como: Sobrecarregar um procedimento que recebe um número indefinido de parâmetros (Visual Basic)

Referência

Sobrecargas (Visual Basic)

Conceitos

Parâmetros opcionais (Visual Basic)

Matrizes de parâmetros (Visual Basic)

Sobrecarga de procedimento (Visual Basic)

Considerações sobre procedimentos de sobrecarga (Visual Basic)

Métodos de extensão (Visual Basic)