Mega Code Archive

 
Categories / VB.Net Tutorial / Class Module
 

Overloaded constructors

Module Tester    Sub Main()       ' use overloaded constructors       Dim time1 As New MyTime()       Dim time2 As New MyTime(2)       Dim time3 As New MyTime(21, 34)       Dim time4 As New MyTime(12, 25, 42)       Dim time5 As New MyTime(27, 74, 99)       Dim time6 As New MyTime(time4) ' use time4 as initial value       Console.WriteLine(time1.ToUniversalString() )       Console.WriteLine(time2.ToUniversalString() )       Console.WriteLine(time3.ToUniversalString())       Console.WriteLine(time4.ToUniversalString())       Console.WriteLine(time5.ToUniversalString())       Console.WriteLine(time6.ToUniversalString())    End Sub ' Main End Module  Class MyTime    Inherits Object    Private mHour As Integer   ' 0 - 23    Private mMinute As Integer ' 0 - 59    Private mSecond As Integer ' 0 - 59    Public Sub New()       SetTime()    End Sub ' New    Public Sub New(ByVal hourValue As Integer)       SetTime(hourValue)    End Sub ' New    Public Sub New(ByVal hourValue As Integer, _       ByVal minuteValue As Integer)       SetTime(hourValue, minuteValue)    End Sub ' New    Public Sub New(ByVal hourValue As Integer, _       ByVal minuteValue As Integer, ByVal secondValue As Integer)       SetTime(hourValue, minuteValue, secondValue)    End Sub ' New    Public Sub New(ByVal timeValue As MyTime)       SetTime(timeValue.mHour, timeValue.mMinute, timeValue.mSecond)    End Sub ' New    Public Sub SetTime(Optional ByVal hourValue As Integer = 0, _       Optional ByVal minuteValue As Integer = 0, _       Optional ByVal secondValue As Integer = 0)       If (hourValue >= 0 AndAlso hourValue < 24) Then          mHour = hourValue       Else          mHour = 0       End If       If (minuteValue >= 0 AndAlso minuteValue < 60) Then          mMinute = minuteValue       Else          mMinute = 0       End If       If (secondValue >= 0 AndAlso secondValue < 60) Then          mSecond = secondValue       Else          mSecond = 0       End If    End Sub ' SetTime    Public Function ToUniversalString() As String       Return String.Format("{0}:{1:D2}:{2:D2}", _          mHour, mMinute, mSecond)    End Function ' ToUniversalString End Class 0:00:00 2:00:00 21:34:00 12:25:42 0:00:00 12:25:42