Section 9.1. Overloading Methods

   

9.1 Overloading Methods

Often you'll want to have more than one method with the same name. The most common example of this is to have more than one constructor. Having more than one constructor with the same name allows you to create the object with different parameters. For example, if you were creating a Time object, you might have circumstances where you want to create the Time object by passing in the date, hours, minutes, and seconds. Other times, you might want to create a Time object by passing in an existing Time object. Still other times, you might want to pass in just a date, without hours and minutes. Overloading the constructor allows you to provide these various options.

Chapter 8 explained that your constructor is automatically invoked when your object is created. Let's return to the Time class created in that chapter. It is possible to create a Time object by passing in a DateTime object to the constructor.

It would be convenient also to allow the client to create a new Time object by passing in year, month, date, hour , minute, and second values. Some clients might prefer one or the other constructor; you can provide both and the client can decide which better fits the situation.

In order to overload your constructor, you must make sure that each constructor has a unique signature . The signature of a method is composed of its name and its parameter list. Two methods differ in their signatures if they have different names or different parameter lists. Parameter lists can differ by having different numbers or types of parameters.

 Public Sub MyMethod(p1 as Integer)  Public Sub MyMethod(p1 as Integer, p2 as Integer) 'different number Public Sub MyMethod(p1 as Integer, s1 as String)  'different types  Public Sub SomeMethod(p1 as Integer)              'different name 

The previous four lines of code show how you might distinguish methods by signature.

The first three methods are all overloads of the myMethod( ) method. The first differs from the second and third in the number of parameters. The second closely resembles the third version, but the second parameter in each is a different type. In the second method, the second parameter (p2) is an integer; in the third method, the second parameter (s1) is a string. These changes to the number or type of parameters are sufficient changes in the signature to allow the compiler to distinguish the methods.

The fourth method differs from the other three methods by having a different name. This is not method overloading, just different methods, but it illustrates that two methods can have the same number and type of parameters if they have different names. Thus, the fourth and first have the same parameter list, but their names are different.

A class can have any number of methods, as long as each one's signature differs from that of all the others. Example 9-1 illustrates a Time class with two constructors, one that takes a DateTime object, and the other that takes six integers.

Example 9-1. Overloading a method
 Option Strict On Imports System Public Class Time    ' private member variables    Private Year As Integer    Private Month As Integer    Private Date As Integer    Private Hour As Integer    Private Minute As Integer    Private Second As Integer    ' public accessor methods    Public Sub DisplayCurrentTime( )       System.Console.WriteLine( _       "{0}/{1}/{2} {3}:{4}:{5}", _       Month, Date, Year, Hour, Minute, Second)    End Sub 'DisplayCurrentTime    ' constructors    Public Sub New(ByVal dt As System.DateTime)       Year = dt.Year       Month = dt.Month       Date = dt.Date       Hour = dt.Hour       Minute = dt.Minute       Second = dt.Second    End Sub 'New    Public Sub New( _    ByVal Year As Integer, _    ByVal Month As Integer, _    ByVal Date As Integer, _    ByVal Hour As Integer, _    ByVal Minute As Integer, _    ByVal Second As Integer)       Me.Year = Year       Me.Month = Month       Me.Date = Date       Me.Hour = Hour       Me.Minute = Minute       Me.Second = Second    End Sub 'New End Class 'Time Module Module1    Sub Main( )       Dim currentTime as System.DateTime = System.DateTime.Now       Dim time1 As New Time(currentTime)       time1.DisplayCurrentTime( )       Dim time2 As New Time(2005, 11, 18, 11, 3, 30)       time2.DisplayCurrentTime( )    End Sub End Module 
  Output:  5/1/2002 8:53:05 11/18/2005 11:3:30 

The Time class in Example 9-1 has two constructors. If a function's signature consisted only of the function name, the compiler would not know which constructors to call when constructing the new Time objects time1 and time2. However, because the signature includes the function parameters and their types, the compiler is able to match the constructor call for time1 with the constructor whose signature requires a DateTime object.

 Dim currentTime As New System.DateTime( ) Dim time1 As New Time(currentTime) time1.DisplayCurrentTime( ) 

Likewise, the compiler is able to associate the time2 constructor call with the constructor method whose signature specifies six integer arguments.

 Dim time2 As New Time(2005, 11, 18, 11, 3, 30) time2.DisplayCurrentTime( ) 

When you overload a method, you must change the signature (i.e., the name, number, or type of the parameters). You are free, as well, to change the return type, but this is optional. Changing only the return type does not overload the method, and creating two methods with the same signature but differing return types will generate a compile error.

   


Learning Visual Basic. NET
Learning Visual Basic .Net
ISBN: 0596003862
EAN: 2147483647
Year: 2002
Pages: 153
Authors: Jesse Liberty

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net