18.12. Overloading Methods and Constructors


Often you'll want to have more than one function with the same name. The most common example of this is to have more than one constructor.

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

18.12.1. Overloaded Methods Different Signatures

The signature of a method is defined by 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 of parameters or different types of parameters. For example, in the following code, the first method differs from the other methods by having a different name. The second differs from the third in the number of parameters, and the third differs from the fourth in the types of parameters:

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

A class can have any number of methods, as long as each one's signature differs from that of all the others.



Programming Visual Basic 2005
Programming Visual Basic 2005
ISBN: 0596009496
EAN: 2147483647
Year: 2006
Pages: 162
Authors: Jesse Liberty

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