mercoledì 27 maggio 2009

Methods Overloading

In object programming (OOP) we can give the same name to multiple methods, provided that we have a different signature, a different set of arguments.

Methods Overloading
Class Calculator

Overloads Public Function Add(a As Integer, b As Integer) As Integer
Add = a + b
End Function

Overloads Public Function Add(a As Double, b As Double) As Double
Add = a + b
End Function

End Class

as you can see we have two methods with the same name 'Add', but with two different signatures, it accepts the first parameter 'a' and 'b' as a integer, the second as duoble.

to use two methods:

Dim counter As Calculator
counter = New Calculator()
' pass two integers
Console.WriteLine(counter.Add(1, 5))
' pass two doubles
Console.WriteLine(counter.Add(1.3, 5.9))

0 commenti:

Posta un commento