Visual Basic 2008 9.0 .NET Examples and Ebook

Object Oriented Programming

Vorig Onderwerp

Procedures and Functions

Method Overloading

Vorig Onderwerp

Inheritance

|

Constructors

Volgend Onderwerp
Exercise

Exercise



Lets start with an collectiontype Persons that manages a collection of Person objects.


Class Person
    Private m_Name As String
    Public Property Name() As String
        Get
            Name = m_Name
        End Get
        Set(ByVal value As String)
            m_Name = value
        End Set
    End Property
End Class
Class Persons
    Protected m_Items As Person()
    Default Public ReadOnly Property Item(ByVal index As Integer) As Person
        Get
            Item = m_Items(index)
        End Get
    End Property
    Protected m_Count As Integer
    Public ReadOnly Property Count() As Integer
        Get
            Count = m_Count
        End Get
    End Property
    Public Sub Add(ByVal person As Person)
        ReDim Preserve m_Items(Count)
        m_Items(Count) = person
        m_Count += 1
    End Sub
End Class
Download Broncode

Suppose we need to be able to query the collection for a Person with a specific name.

Therefore we could make as derived class QueryablePersons with an extra member Item which delivers a Person based on a specific name.

Item is a logical identifier, after all this member should deliver an item of the collection.

From the base class 'Persons' we've already inherited an 'Item' member.
To make a call to on of the members Item unambiguous ( clear which Item is called ) we need to have differences in the arguments of this members.


Class QueryablePersons : Inherits Persons
    Default Public Overloads ReadOnly Property Item( _
                                               ByVal name As String) As Person
        Get
            Dim index As Integer, found As Boolean
            Do While index < Count AndAlso Not found
                If m_Items(index).Name = name Then
                    Item = m_Items(index)
                    found = True
                Else
                    index += 1
                End If
            Loop
        End Get
    End Property
End Class
Module Client
    Sub Main()
        Dim person1 As Person = New Person
        person1.Name = "John"
        '
        Dim person2 As Person = New Person
        person2.Name = "Jane"
        '
        Dim queryablePersons1 As QueryablePersons = New QueryablePersons
        queryablePersons1.Add(person1)
        queryablePersons1.Add(person2)
        '
        Console.WriteLine(queryablePersons1.Item(1) Is person2)
        Console.WriteLine(queryablePersons1.Item("Jane") Is person2)
        Console.WriteLine(queryablePersons1.Item("Jane").Name = "Jane")
        '
        Console.ReadLine()
    End Sub
End Module
Download Broncode

Output :

 True
 True
 True

To create multiple members ( in the same class ) with the same identifier "method overloading" is used.

Item ( with an Integer-argument ) is inherited into QueryablePersons and overloaded with Item ( with a String-argument ).

In case an inherited member is overloaded, the Overloads keyword has to be used in the definition of that members ( in the derived class ).
If the keyword Overloads is not used, "shadowing" is used, which is not the same as method overloading. Further on more details about shadowing, usually it is better to avoid shadowing.

When Item is called the compiler will chose the best fitting version based on the arguments. When a call is made with an Integer-argument (1) the compiler interprets this as a call to Item of Person. When a String-argument is provided, the compiler interprets this as a call to Item of QueryablePersons.

How different versions of a implementation member should differ to maintain unambiguous calls for the compiler is defined in the "overload resolution" of the used language.
In Visual Basic .NET all overloaded members should have a difference in type of argument, number of arguments, or type sequence of arguments.

Don't use method overloading to often. Certainly in combination with inheritance it can lead to unreadable/unpredictable code. Further on more about this difficulty.


Exercise


Task :

Create a class Counter. Counter object can be queried for their value. It should be possible to raise and lower the values of the counters.
Raising and lowering a counter, will always lead to adding or subtracting one from the value of that counter.

Also write a SpecialCounter class. This is a specialisation of Counter. Here the class also has members to raise and lower the counter with a specific value.


Solution :


Class Counter
    Protected m_Value As Integer
    Public Function GetValue() As Integer
        GetValue = m_Value
    End Function
    Public Sub Raise()
        m_Value += 1
    End Sub
    Public Sub Lower()
        m_Value -= 1
    End Sub
End Class
Class SpecialCounter : Inherits Counter
    Public Sub SetValue(ByVal value As Integer)
        m_Value = value
    End Sub
    Public Overloads Sub Raise(ByVal stapValue As Integer)
        m_Value += stapValue
    End Sub
    Public Overloads Sub Lower(ByVal stapValue As Integer)
        m_Value -= stapValue
    End Sub
End Class
Module ExerciseSolution
    Sub Main()
        Dim counter1 As Counter = New Counter
        Console.WriteLine(counter1.GetValue() = 0)
        counter1.Raise()
        Console.WriteLine(counter1.GetValue() = 1)
        counter1.Raise()
        Console.WriteLine(counter1.GetValue() = 2)
        counter1.Lower()
        Console.WriteLine(counter1.GetValue() = 1)
        counter1.Lower()
        Console.WriteLine(counter1.GetValue() = 0)
        '
        Dim specialCounter1 As SpecialCounter = New SpecialCounter
        Console.WriteLine(specialCounter1.GetValue() = 0)
        specialCounter1.SetValue(10)
        Console.WriteLine(specialCounter1.GetValue() = 10)
        specialCounter1.Raise()
        Console.WriteLine(specialCounter1.GetValue() = 11)
        specialCounter1.Raise()
        Console.WriteLine(specialCounter1.GetValue() = 12)
        specialCounter1.Lower()
        Console.WriteLine(specialCounter1.GetValue() = 11)
        specialCounter1.Lower()
        Console.WriteLine(specialCounter1.GetValue() = 10)
        specialCounter1.Raise(5)
        Console.WriteLine(specialCounter1.GetValue() = 15)
        specialCounter1.Lower(8)
        Console.WriteLine(specialCounter1.GetValue() = 7)
        '
        Console.ReadLine()
    End Sub
End Module
Download Broncode

Output :

 True
 True
 True
 True
 True
 True
 True
 True
 True
 True
 True
 True
 True




This version ( published on 2008-06-24 ) is printed from http://www.studyvb.com, visit the website for more recent information.

Updated On : 2008-05-29

Download Broncode

Published On : 2008-06-24

Method Overloading

Vorig Onderwerp

Inheritance

|

Constructors

Volgend Onderwerp

Object Oriented Programming

Vorig Onderwerp

Procedures and Functions

Nederlands  Nederlands

Add to favorites (IE).