Recipe 3.14. Creating a Method That Accepts Different Sets of ArgumentsProblemYou have a great function that generates its results based on one type of data, but you'd like to reuse that function with other data types or argument signatures. SolutionUse method overloading to provide different versions of the same method. Discussion
You may sometimes write applications that communicate with Oracle databases. Supplying dates to Oracle SQL statements is frequently done using Oracle's
TO_DATE
function. When building SQL statements in my .NET application, you can prepare a Date variable for use in Oracle by first wrapping it in a
TO_DATE
function. There are other times when all you have is a date in a
Public
Overloads Function ToOracleDate( _
ByVal origDate As Date) As String
Return "TO_DATE('" & Format(origDate, "MM/dd/yyyy") & _
"', 'MM/DD/YYYY')"
End Function
Public Overloads Function ToOracleDate( _
ByVal origDate As String) As String
If (Trim(origDate) = "") Then
Return "NULL"
Else
Return ToOracleDate(CDate(origDate))
End If
End Function
The
Overloads
keyword informs Visual Basic that you are trying to overload a single function
The .NET Framework makes
Public Function InStr(ByVal String1 As String, _ ByVal String2 As String) As Integer
The second variation
Public Function InStr(ByVal Start As Integer, _ ByVal String1 As String, ByVal String2 As String) As Integer
Since Visual Basic does not support optional arguments
Overloading
is different from
overriding
. Overriding occurs only in inheritance relationships, when a function in a derived class alters or
There are no fixed limits on the number of overloads you can use in a single method. And while constructors ( Sub New procedures) also use a form of overloading, they do not require the Overloads keyword. See AlsoSee Recipe 3.6 for information on overloading using class constructors. |
Recipe 3.15. Using Standard Operators for Nonstandard PurposesProblemThe basic Visual Basic operators, such as the addition operator (+), seem so useful that you would like to use them for your own custom classes. SolutionUse operator overloading , a new feature in Visual Basic 2005, to allow your own classes to interact with each other through the standard Visual Basic operators. Discussion
Operator overloading extends method overloading to include the standard Visual Basic operators. In a way, it treats operators such as +, *, and Or as method
Class Specimen If your application supports the idea of combining two specimens, resulting in a merged yet single larger specimen, it would be great to be able to use the addition operator to merge two distinct specimens into a single combined specimen: Dim part1 As New Specimen Dim part2 As New Specimen Dim combinedParts As Specimen '…later… combinedParts = part1 + part2 To add support for addition to this class, overload the + operator by adding an Operator definition to the class: Public Shared Operator +(ByVal firstPart As Specimen, _ ByVal secondPart As Specimen) As Specimen Dim mergedSpecimen As New Specimen ' ----- Add logic to merge the two parts, then… Return mergedSpecimen End Operator You can include different input or output types in the overloaded function, as long as at least one input or output matches the class in which the overload appears: Public Shared Operator +(ByVal singlePage As Page, _ ByVal sourceBook As Book) As Book ' ----- Adds a page to a book. End Operator All overloaded operators must include the Shared keyword in the definition.
For unary operators, such as the
Not
operator, only a single argument is sent to the overloaded function. Table 3-1 lists the
Table 3-1. Overloadable operators
You can overload overloaded operators. That is, you can include multiple overloads for, say, the addition (
+
) operator in a single class, as long as the argument signatures
While operator overloading can make your code more straightforward, it can also add a level of confusion, since you will be using operators in a way that is not part of the standard language usage. Where there is the possibility of confusion, add meaningful comments to the code to guide the reader through the rough spots. See AlsoRecipe 3.14 discusses standard method overloading. |