Previous section   Next section

5.9 Overloading Methods and Constructors

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

Let's return to the Time class you created in Example 5-3. It might be convenient to create a Time class object by passing in a DateTime object (provided by the Framework). On the other hand, it might also be convenient to pass in the hour, minute, second, and date. 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.

Of course, constructors must all have the same name, as every constructor is named with the name of the class. Therefore, to overload the constructor, you must vary the parameter list. Parameter lists can differ by having different numbers or types of parameters.

The following four lines of code show how you might distinguish methods by varying the signature:

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 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 5-9 illustrates a Time class with two constructors, one that takes a DateTime object, and the other that takes six integers.

Example 5-9. Overloading a method
Option Strict On
Imports System

Public Class Time
   ' private member variables
   Private year As Integer
   Private month As Integer
   Private dayOfMonth As Integer
   Private hour As Integer
   Private minute As Integer
   Private second As Integer


   ' public accessor methods
   Public Sub DisplayCurrentTime( )
      Console.WriteLine( _
      "{0}/{1}/{2} {3}:{4}:{5}", _
      month, dayOfMonth, year, hour, minute, second)
   End Sub 'DisplayCurrentTime


   ' constructors
   Public Sub New(ByVal dt As DateTime)
      year = dt.Year
      month = dt.Month
      dayOfMonth = dt.Day
      hour = dt.Hour
      minute = dt.Minute
      second = dt.Second
   End Sub 'New


   Public Sub New( _
   ByVal year As Integer, _
   ByVal month As Integer, _
   ByVal dayOfMonth As Integer, _
   ByVal hour As Integer, _
   ByVal minute As Integer, _
   ByVal second As Integer)
      Me.year = year
      Me.month = month
      Me.dayOfMonth = dayOfMonth
      Me.hour = hour
      Me.minute = minute
      Me.second = second
   End Sub 'New

End Class 'Time


Module Module1

   Sub Main( )
      Dim currentTime As DateTime = 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 5-9 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 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 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.


  Previous section   Next section
Top