Ahad, 23 September 2012

Time Class.vb



Form1.vb

Public Class TimeTest
    Dim time As New Time() ' construct Time with zero arguments

    ' invoked when user clicks the Add 1 to Second button
    Private Sub addSecondButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addSecondButton.Click
        time.Second = (time.Second + 1) Mod 60  ' add 1 to Second
        setSecondTextbox.Text = Convert.ToString(time.Second)

        ' add one minute if 60 seconds have passed

        If time.Second = 0 Then
            time.Minute = (time.Minute + 1) Mod 60  ' add 1 to Minute
            setMinuteTextbox.Text = Convert.ToString(time.Minute)

            ' add one hour if 60 minutes have passed

            If time.Minute = 0 Then
                time.Hour = (time.Hour + 1) Mod 24  ' add 1 to Hour
                setHourTextbox.Text = Convert.ToString(time.Hour)
            End If
        End If

        UpdateDisplay()     ' update the text in output1Label and output2Label
    End Sub     ' addSecondButton_Click

    ' handle event when setHourTestox's text changes
    Private Sub setHourTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles setHourTextbox.TextChanged
        time.Hour = Convert.ToInt32(setHourTextbox.Text)
        UpdateDisplay()  ' update the text in output1Label and output2Label
    End Sub  ' setHourTextBox_TextChanged

    ' handle event when setMinuteTextBox's text changes
    Private Sub setMinuteTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles setMinuteTextbox.TextChanged
        time.Minute = Convert.ToInt32(setMinuteTextbox.Text)
        UpdateDisplay()  ' update the text in output1Label and output2Label
    End Sub  ' setMinuteTextBox_TextChanged

    ' handle event when setSecondTextBoxt's text changed
    Private Sub setSecondTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles setSecondTextbox.TextChanged
        time.Second = Convert.ToInt32(setSecondTextbox.Text)
        UpdateDisplay()  ' update the text in output1Label and output2Label
    End Sub  ' setSecondTextBox_TextChanged

    ' update time display
    Private Sub UpdateDisplay()
        setHourTextbox.Text = Convert.ToString(time.Hour)
        setMinuteTextbox.Text = Convert.ToString(time.Minute)
        setSecondTextbox.Text = Convert.ToString(time.Second)
        output1Label.Text = String.Format("Hour: {0}; Minute: {1}; Second: {2}", time.Hour, time.Minute, time.Second)
        output2Label.Text = String.Format("Standard time is: {0}; Universal Time is: {1}", time.ToString(), time.ToUniversalString())
    End Sub  ' UpdateDisplay

    Private Sub output1Label_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles output1Label.Click

    End Sub

    Private Sub output2Label_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles output2Label.Click

    End Sub

    Private Sub TimeTest_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
End Class  'TimeTest



Time.vb

Public Class Time
    Inherits Object 'if omitted, Object is inherited implicitly

    ' declare Integer instance variables for the hour, minute and second
    Private hourValue As Integer    ' 0 - 23
    Private minuteValue As Integer    ' 0 - 59
    Private secondValue As Integer    ' 0 - 59

    ' Time constructor initializes instance variables
    ' when a Time object is created
    Public Sub New()
        SetTime(12, 0, 0) ' initialize hour to noon; minute, second to 0
    End Sub     ' New

    ' set a new time value using universal time, check validity of the
    ' data, set invalid hour to noon, set invalid minute, second to zero
    Public Sub SetTime(ByVal hh As Integer, ByVal mm As Integer, ByVal ss As Integer)
        Hour = hh   ' set hour value using Hour property
        Minute = mm ' set minute value using Minute property
        Second = ss ' set second value using Second property
    End Sub     ' SetTime

    ' property Hour
    Public Property Hour() As Integer
        Get ' return hourValue
            Return hourValue
        End Get
        Set(ByVal value As Integer) ' set hourValue

            If (value >= 0 AndAlso value < 24) Then ' in range 0 - 23
                hourValue = value   ' value is valid
            Else    ' value is invalid
                hourValue = 12  ' set to default of noon
            End If

        End Set
    End Property    ' Hour

    Public Property Minute() As Integer
        Get ' return minuteValue
            Return minuteValue
        End Get
        Set(ByVal value As Integer) ' set minuteValue

            If (value >= 0 AndAlso value < 60) Then ' in range 0 - 59
                minuteValue = value   ' value is valid
            Else    ' value is invalid
                minuteValue = 0  ' set to default of 0
            End If

        End Set
    End Property    ' Minute

    Public Property Second() As Integer
        Get ' return secondValue
            Return secondValue
        End Get
        Set(ByVal value As Integer) ' set secondValue

            If (value >= 0 AndAlso value < 60) Then ' in range 0 - 59
                secondValue = value   ' value is valid
            Else    ' value is invalid
                secondValue = 0  ' set to default of 0
            End If

        End Set
    End Property    ' Second

    ' convert Time to a String in universal-time (24-hour clock) format
    Public Function ToUniversalString() As String
        Return String.Format("{0}:{1:D2}:{2:D2}", Hour, Minute, Second)
    End Function    'ToUniversalString

    ' convert Time to a String in standard-time (12-hour clock) format
    Public Overrides Function ToString() As String
        Dim suffix As String    ' AM or PM suffix
        Dim standardHour As Integer ' a standard hour in the range 1 - 12

        ' determine whether the 12-hour clock suffix should be AM or PM
        If Hour < 12 Then
            suffix = "AM"
        Else
            suffix = "PM"
        End If

        ' convert hour from universal-time format to standard-time format
        If (Hour = 12 OrElse Hour = 0) Then
            standardHour = 12
        Else
            standardHour = Hour Mod 12  ' 1 through 11, AM or PM
        End If

        Return String.Format("{0}:{1:D2}:{2:D2} {3}", standardHour, Minute, Second, suffix)
    End Function    ' ToString
End Class   ' Time

Output




Tiada ulasan:

Catat Ulasan

LinkWithin

Related Posts Plugin for WordPress, Blogger...