Recipe 3.14. Creating a Method That Accepts Different Sets of Arguments


You 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.


Use method overloading to provide different versions of the same method.


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 user-entered string format, and you need to prepare that date for use by Oracle. To support both original date and string data values, you can use an overloaded Oracle preparation function:

 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 name with two different argument signature variations. In this example, the string version calls the date version for some of its processing. This sharing of processing logic can help keep your code simple even when using multiple overloads.

The .NET Framework makes extensive use of method overloading, including over-loads of some Visual Basic features. The InStr() function, which locates a smaller string within a larger one, uses overloading to support the interesting syntax it inherited from Visual Basic 1.0. The basic syntax uses two strings, the one being searched and the one being sought:

 Public Function InStr(ByVal String1 As String, _    ByVal String2 As String) As Integer 

The second variation inserts an Integer starting position as the first argument:

 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 anywhere but at the end of an argument list, this function uses overloading to support the argument variety.

Overloading is different from overriding. Overriding occurs only in inheritance relationships, when a function in a derived class alters or replaces the logic for an identical function in a base class. Overridden functions must have the same argument signature in both the base and derived classes.

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 Also

See Recipe 3.6 for information on overloading using class constructors.

Visual Basic 2005 Cookbook(c) Solutions for VB 2005 Programmers
Visual Basic 2005 Cookbook: Solutions for VB 2005 Programmers (Cookbooks (OReilly))
ISBN: 0596101775
EAN: 2147483647
Year: 2006
Pages: 400

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: