>Namespace Framework > > Public Class CalcAge > > Public dBirth As Date = Nothing > Public dTarget As Date = Nothing > Public nDay As Integer = 0 > Public nMonth As Integer = 0 > Public nYear As Integer = 0 > Private oApp As Framework.App = Nothing > Private oProcess As Framework.LXProcess = Nothing > > ' This is when we access the class in a desktop mode > Public Sub New(ByVal toApplication As Framework.App) > oApp = toApplication > End Sub > > ' This is when we access the class in a Web or Web Service mode > Public Sub New(ByVal toProcess As Framework.LXProcess) > oProcess = toProcess > oApp = oProcess.oApp > End Sub > > ' Calculate age > Public Function CalcAge() As Boolean > Dim ldBirth As Date = Date.Now > Dim ldBirthAdjusted As Date = Date.Now > Dim ldTarget As Date = Nothing > Dim ldTemp As Date = Date.Now > Dim lnDrop As Integer = 0 > > ' Reset the values > nDay = 0 > nMonth = 0 > nYear = 0 > > ' Work with a local variable > ldBirthAdjusted = dBirth > ldTarget = dTarget > > ' If the birth date is greater than the target date > If dBirth > ldTarget Then > ldTemp = ldTarget > ldTarget = ldBirthAdjusted > ldBirthAdjusted = ldTemp > End If > > Try > ldBirth = New Date(ldTarget.Year, ldBirthAdjusted.Month, ldBirthAdjusted.Day) > Catch loError As Exception > > ' If the date cannot be calculated, this is because we have a leap year > ldBirth = New Date(ldTarget.Year, 3, 1) > > ' If we have a leap year > If ldTarget.Month > 2 Then > lnDrop = 1 > End If > > End Try > > ' If the birth date is greater than the target date > If ldBirth > ldTarget Then > nYear = ldTarget.Year - ldBirthAdjusted.Year - 1 > Else > nYear = ldTarget.Year - ldBirthAdjusted.Year > End If > > ' If the birth date day is greater than the target day > If ldBirthAdjusted.Day > ldTarget.Day Then > nMonth = (ldTarget.Month - ldBirthAdjusted.Month + 12 - 1) Mod 12 > Else > nMonth = (ldTarget.Month - ldBirthAdjusted.Month + 12) Mod 12 > End If > > ldTemp = New Date(ldBirthAdjusted.Year + nYear, ldBirthAdjusted.Month, ldBirthAdjusted.Day) > > nDay = Math.Abs(DateDiff(DateInterval.Day, ldTarget, ldTemp.AddMonths(nMonth))) - lnDrop > > Return True > End Function > > End Class > >End Namespace >