JavaScript Editor JavaScript Editor     JavaScript Debugger

Previous Section Next Section

Main Page

Overloading Methods and Properties

In OOP, methods and properties can be overloaded, which means that you can define a method or property multiple times with different argument lists. For example, say you had a method named Draw that can draw rectangles and triangles. For rectangles, you might need only four arguments in your argument list—the x and y coordinates of the upper-left and lower-right corners of the rectangle. However, for triangles, you might need six arguments, corresponding to the x and y coordinates of the three vertices of the triangle. To handle this, you can overload Draw to handle four or six arguments just by defining the method twice with two different argument lists (note, by the way, that to be different, an argument list need not have a different number of arguments—arguments are considered different if they're of different data types too).


Don't confuse overloading with overriding; overriding is something that happens with inheritance, where a base class member is replaced by different member in the derived class. We'll see overriding in Chapter 12.

Here's an example named Overloading on the CD-ROM. In this case, I'll create a class named Notifier with a method named Display, which will display a message box. Display will have two versions—you pass the text to display in the message box to the first version, and the text and a message box icon to the second. Here, then, is all you do to overload a method—just define it a number of times with different argument lists—note that I'm adding two buttons to call the two versions of Display:

    Dim notifierObject As New Notifier()

    Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click
        notifierObject.Display("Hello from Visual Basic!")
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button2.Click
        notifierObject.Display("Hello from Visual Basic!", _
    End Sub

    Public Class Notifier
        Public Sub Display(ByVal Message As String)
        End Sub

        Public Sub Display(ByVal Message As String, ByVal Icon _
            As MsgBoxStyle)
            MsgBox(Message, Icon)
        End Sub
    End Class

How does Visual Basic know which overloaded version to use? All it has to do is to check the argument list you're passing to the method and find the version of the method that has the same number and types of arguments, in the same order. And that's all it takes—you can see the results in Figure 11.3.

Figure 11.3: Overloading a method.

There is also an Overloads keyword that you can use to indicate that you're overloading a method or property. You don't need to use Overloads when you're overloading members of the same class, but it becomes important when you'reworking with inheritance.

Related solution:

Found on page:

Overloading Base Class Members


Previous Section Next Section

JavaScript Editor Free JavaScript Editor     JavaScript Editor