Procedure Overloading (Visual Basic)
Overloading a procedure means defining it in multiple versions, using the same name but different parameter lists. The purpose of overloading is to define several closely related versions of a procedure without having to differentiate them by name. You do this by varying the parameter list.
When you overload a procedure, the following rules apply:
Same Name. Each overloaded version must use the same procedure name.
Different Signature. Each overloaded version must differ from all other overloaded versions in at least one of the following respects:
Number of parameters
Order of the parameters
Data types of the parameters
Number of type parameters (for a generic procedure)
Return type (only for a conversion operator)
Together with the procedure name, the preceding items are collectively called the signature of the procedure. When you call an overloaded procedure, the compiler uses the signature to check that the call correctly matches the definition.
Items Not Part of Signature. You cannot overload a procedure without varying the signature. In particular, you cannot overload a procedure by varying only one or more of the following items:
Procedure modifier keywords, such as
Parameter or type parameter names
Type parameter constraints (for a generic procedure)
Parameter modifier keywords, such as
Whether it returns a value
The data type of the return value (except for a conversion operator)
The items in the preceding list are not part of the signature. Although you cannot use them to differentiate between overloaded versions, you can vary them among overloaded versions that are properly differentiated by their signatures.
Late-Bound Arguments. If you intend to pass a late bound object variable to an overloaded version, you must declare the appropriate parameter as Object.
Multiple Versions of a Procedure
Suppose you are writing a
Sub procedure to post a transaction against a customer's balance, and you want to be able to refer to the customer either by name or by account number. To accommodate this, you can define two different
Sub procedures, as in the following example:
Sub postName(ByVal custName As String, ByVal amount As Single) ' Insert code to access customer record by customer name. End Sub Sub postAcct(ByVal custAcct As Integer, ByVal amount As Single) ' Insert code to access customer record by account number. End Sub
An alternative is to overload a single procedure name. You can use the Overloads keyword to define a version of the procedure for each parameter list, as follows:
Overloads Sub post(ByVal custName As String, ByVal amount As Single) ' Insert code to access customer record by customer name. End Sub Overloads Sub post(ByVal custAcct As Integer, ByVal amount As Single) ' Insert code to access customer record by account number. End Sub
If you also wanted to accept a transaction amount in either
Single, you could further overload
post to allow for this variation. If you did this to each of the overloads in the preceding example, you would have four
Sub procedures, all with the same name but with four different signatures.
Advantages of Overloading
The advantage of overloading a procedure is in the flexibility of the call. To use the
post procedure declared in the preceding example, the calling code can obtain the customer identification as either a
String or an
Integer, and then call the same procedure in either case. The following example illustrates this:
Imports MSVB = Microsoft.VisualBasic
Dim customer As String Dim accountNum As Integer Dim amount As Single customer = MSVB.Interaction.InputBox("Enter customer name or number") amount = MSVB.Interaction.InputBox("Enter transaction amount") Try accountNum = CInt(customer) Call post(accountNum, amount) Catch Call post(customer, amount) End Try
- How to: Define Multiple Versions of a Procedure
- How to: Call an Overloaded Procedure
- How to: Overload a Procedure that Takes Optional Parameters
- How to: Overload a Procedure that Takes an Indefinite Number of Parameters
- Considerations in Overloading Procedures
- Overload Resolution
- Generic Types in Visual Basic