Interface with Property : Interface « Class Module « VB.Net Tutorial






Class Tester

   Shared Sub Main()

      Dim tree As New Plant(1976)
      Dim person As New Animal("B", "J", 1983)

      Dim iAgeArray As Markable() = New Markable(1) {}

      iAgeArray(0) = tree
      iAgeArray(1) = person

      Console.WriteLine(tree.ToString() & ": " & _
         tree.Name & vbCrLf & "Age is " & tree.Age)

      Console.WriteLine(person.ToString() & ": " & _
         person.Name & vbCrLf & "Age is " & person.Age)

      Dim ageReference As Markable

      For Each ageReference In iAgeArray
         Console.WriteLine(ageReference.Name & ": " & _
            "Age is " & ageReference.Age)
      Next

   End Sub ' Main

End Class


Public Interface Markable

   ReadOnly Property Age() As Integer
   ReadOnly Property Name() As String

End Interface

Public Class Animal
   Implements Markable

   Private mYearBorn As Integer
   Private mFirstName As String
   Private mLastName As String

   Public Sub New(ByVal firstNameValue As String, _
      ByVal lastNameValue As String, _
      ByVal yearBornValue As Integer)

      mFirstName = firstNameValue
      mLastName = lastNameValue

      mYearBorn = yearBornValue
   End Sub ' New

   ReadOnly Property Age() As Integer _
      Implements Markable.Age
      Get
         Return Date.Now.Year - mYearBorn
      End Get

   End Property ' Age

   ReadOnly Property Name() As String _
      Implements Markable.Name
      Get
         Return mFirstName & " " & mLastName
      End Get
   End Property ' Name

End Class

Public Class Plant
   Implements Markable

   Private mRings As Integer
   Public Sub New(ByVal yearPlanted As Integer)
      mRings = Date.Now.Year - yearPlanted
   End Sub ' New

   Public Sub AddRing()
      mRings += 1
   End Sub ' AddRing
   ReadOnly Property Age() As Integer _
      Implements Markable.Age
      Get
         Return mRings
      End Get

   End Property ' Age

   ReadOnly Property Name() As String _
      Implements Markable.Name
      Get
         Return "Tree"
      End Get

   End Property ' Name

End Class
Plant: Tree
Age is 31
Animal: B J
Age is 24
Tree: Age is 31
B J: Age is 24








6.44.Interface
6.44.1.Implement Interface
6.44.2.Interface with two methods
6.44.3.One Class implements two interfaces
6.44.4.Interface Inherits Interface
6.44.5.Interface with Property
6.44.6.Inheritance And Interfaces
6.44.7.Reimplement interface
6.44.8.Public Sub aMethod() Implements Interface1.Method, Interface2.Method
6.44.9.Two classes implement one interface
6.44.10.Implements two interfaces and WriteOnly Property