' Visual Basic 2008 9.0 .NET Examples - Method Overloading - Object Oriented Programming : 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 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 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 ' Visit www.studyvb.com for more examples. Copyright 2003-2008 De Wolf.