Training
Module
Call methods from the .NET Class Library using C# - Training
Use functionality in the .NET Class Library by calling methods that return values, accept input parameters, and more.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
The Visual Basic compiler must decide which overload to call when a procedure is defined in several overloaded versions. It decides by performing the following steps:
On
or Off
.The following illustration shows the process that determines which of a set of overloaded versions to call.
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 can't eliminate any of the overloads based on 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 can't 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.
If two overloads of a procedure have identical signatures except that the last parameter is declared Optional in one and ParamArray 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 |
.NET feedback
.NET is an open source project. Select a link to provide feedback:
Training
Module
Call methods from the .NET Class Library using C# - Training
Use functionality in the .NET Class Library by calling methods that return values, accept input parameters, and more.