Mega Code Archive

 
Categories / VB.Net Tutorial / Class Module
 

Do calculation in Property getter

Imports System Imports System.Data.OleDb Imports System.ComponentModel.Component Class Employee   Private MName As String   Private MDob As DateTime   Private MCompany As String   Public Sub New(ByVal name As String, ByVal dob As DateTime)     MName = name     MDob = dob     MCompany = "ABC"   End Sub   Public Property Name() As String     Get       Return MName     End Get     Set(ByVal Value As String)       MName = Value     End Set   End Property   Public ReadOnly Property Age() As Integer     Get       Dim today As DateTime = DateTime.Now       Dim Years As Integer = DateTime.Now.Year - mDob.Year       If (today.Month < MDob.Month) Or _          (today.Month = MDob.Month And _            today.Day < MDob.Day) Then         Years -= 1       End If       Return years     End Get   End Property End Class Module Test   Sub Main()     Dim AEmployee As New Employee("A", New DateTime(1964, 12, 3))     Console.WriteLine("{0} is {1}", AEmployee.Name, AEmployee.Age)   End Sub End Module A is 42