Mega Code Archive

 
Categories / VB.Net Tutorial / Class Module
 

Interface with Property

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