Overloaded Properties and Methods
Overloading is the creation of more than one procedure, instance constructor, or property in a class with the same name but different argument types.
Overloading is especially useful when your object model dictates that you employ identical names for procedures that operate on different data types. For example, a class that can display several different data types could have
Display procedures that look like this:
Overloads Sub Display(ByVal theChar As Char) ' Add code that displays Char data. End Sub Overloads Sub Display(ByVal theInteger As Integer) ' Add code that displays Integer data. End Sub Overloads Sub Display(ByVal theDouble As Double) ' Add code that displays Double data. End Sub
Without overloading, you would need to create distinct names for each procedure, even though they do the same thing, as shown next:
Sub DisplayChar(ByVal theChar As Char) ' Add code that displays Char data. End Sub Sub DisplayInt(ByVal theInteger As Integer) ' Add code that displays Integer data. End Sub Sub DisplayDouble(ByVal theDouble As Double) ' Add code that displays Double data. End Sub
Overloading makes it easier to use properties or methods because it provides a choice of data types that can be used. For example, the overloaded
Display method discussed previously can be called with any of the following lines of code:
Display("9"C) ' Call Display with a literal of type Char. Display(9) ' Call Display with a literal of type Integer. Display(9.9R) ' Call Display with a literal of type Double.
At run time, Visual Basic .NET calls the correct procedure based on the data types of the parameters you specify.
Note Overloading, overriding, and shadowing are similar concepts that can be easy to confuse. See Introduction to Objects in Visual Basic for more information.
You create an overloaded member for a class by adding two or more properties or methods with the same name. Except for overloaded derived members, each overloaded member must have different parameter lists, and the following items cannot be used as a differentiating feature when overloading a property or procedure:
- Modifiers, such as ByVal or ByRef, that applies to a member, or parameters of the member.
- Names of parameters
- Return types of procedures
The Overloads keyword is optional when overloading, but if any overloaded member uses the Overloads keyword, then all other overloaded members with the same name must also specify this keyword.
Derived classes can overload inherited members with members that have identical parameters and parameter types, a process known as shadowing by name and signature. If the Overloads keyword is used when shadowing by name and signature, the derived classes implementation of the member will be used instead of the implementation in the base class, and all other overloads for that member will be available to instances of the derived class.
If the Overloads keyword is omitted when overloading an inherited member with a member that has identical parameters and parameter types, then the overloading is called shadowing by name. Shadowing by name replaces the inherited implementation of a member, and makes all other overloads unavailable to instances of the derived class, and its decedents.
The Overloads and Shadows modifiers cannot both be used with the same property or method.
The following example creates overloaded methods that accept either a String or Decimal representation of a dollar amount and return a string containing the sales tax.
To use this example to create an overloaded method
- Open a new project and add a class named
- Add the following code to the
Public Class TaxClass Overloads Function TaxAmount(ByVal decPrice As Decimal, _ ByVal TaxRate As Single) As String TaxAmount = "Price is a Decimal. Tax is $" & _ (CStr(decPrice * TaxRate)) End Function Overloads Function TaxAmount(ByVal strPrice As String, _ ByVal TaxRate As Single) As String TaxAmount = "Price is a String. Tax is $" & _ CStr((CDec(strPrice) * TaxRate)) End Function End Class
- Add the following procedure to your form.
Sub ShowTax() Const TaxRate As Single = 0.08 ' 8% tax rate Dim strPrice As String = "64.00" ' $64.00 Purchase as a String. Dim decPrice As Decimal = 64 ' $64.00 Purchase as a Decimal. Dim aclass As New taxclass() 'Call the same method with two diferent kinds of data. MessageBox.Show(aclass.TaxAmount(strPrice, TaxRate)) MessageBox.Show(aclass.TaxAmount(decPrice, TaxRate)) End Sub
- Add a button to your form and call the
ShowTaxprocedure from the
Button1_Clickevent of the button.
- Run the project and click the button on the form to test the overloaded
At run time, the compiler chooses the appropriate overloaded function that matches the parameters being used. When you click the button the overloaded method is called first with a
Price parameter that is a string and the message: "Price is a String. Tax is $5.12" is displayed.
TaxAmount is called with a Decimal value the second time and the message, "Price is a Decimal. Tax is $5.12" is displayed.