Time algorithm

birdboy30

New member
Joined
Jul 7, 2007
Messages
2
Programming Experience
Beginner
I need help developing a time algorithm. I'm supposed to create a clock that advances/rollsback time the desired amount typed in a text box. The clock keeps time only in seconds, and I can't seem to figure out how to extract the hours, minutes, etc. Also, I don't know how to set the figures

ex. Public Sub setTime(ByVal hrs As Integer, ByVal mins As Integer, ByVal secs As Integer)

and the same with getTime(ByRef...) because you can't have a return type in sub procedures, right? Any help will be appreciated, and I'll be glad to provide anymore information.
 
You can have value returned by using ByRef instead of ByVal

if I have a sub...

VB.NET:
Sub Add(ByVal x as integer, ByVal y as integer, ByRef Total as integer)
Total = x+y
end sub

and I call it like

VB.NET:
Dim total as integer

add(x,y,total)

then total WILL have the correct value in it, because I used Byref.

Byref changes the value of the original varible you referenced, byval makes a copy of that variable, and only that copy is changed.
:cool:
 
Problem with time algorithm

I've created a time algorithm that is supposed to update according to the amount of seconds entered, but it doesn't seem to be working correctly. Whenever I hit 30 seconds, the minutes increase by 1, and then mins update ever 60 seconds after that. The same with hours in regards to minutes. Could anyone please look over my code and give me some sort of reason why?

VB.NET:
Public Class MyTime
    Private timeInSeconds As Long = 0

    REM Times in seconds
    Const MAX_TIME As Integer = 3600000, ONE_HOUR As Integer = 3600, ONE_MIN As Integer = 60

    REM Allow users to set the time components
    Public Sub setTime(ByVal hrs As Integer, ByVal mins As Integer, ByVal secs As Integer)
        Me.timeInSeconds = (((hrs * ONE_HOUR) + (mins * ONE_MIN)) + secs)
    End Sub

    REM Allow users to get the time components
    Public Sub getTime(ByRef hrs As Integer, ByRef mins As Integer, ByRef secs As Integer)
        secs = (Me.timeInSeconds Mod ONE_MIN)
        mins = ((Me.timeInSeconds Mod ONE_HOUR) / ONE_MIN)
        hrs = (Me.timeInSeconds / ONE_HOUR)
    End Sub

    REM Allow users to access the hour component
    Public Property hour() As Integer
        Get
            Return (Me.timeInSeconds / ONE_HOUR)
        End Get
        Set(ByVal value As Integer)
            Dim num As Integer = (Me.timeInSeconds / ONE_HOUR)
            Me.timeInSeconds = ((Me.timeInSeconds - (num * ONE_HOUR)) + (value * ONE_HOUR))
        End Set
    End Property

    REM Allow users to access the minute component
    Public Property minute() As Integer
        Get
            Return (Me.timeInSeconds Mod ONE_HOUR) / ONE_MIN
        End Get
        Set(ByVal value As Integer)
            Dim num As Integer = ((Me.timeInSeconds Mod ONE_HOUR) / ONE_MIN)
            Me.timeInSeconds = ((Me.timeInSeconds - (num * ONE_MIN)) + (value * ONE_MIN))
        End Set
    End Property

    REM Allow users to access the second component
    Public Property second() As Integer
        Get
            Return (Me.timeInSeconds Mod ONE_MIN)
        End Get
        Set(ByVal value As Integer)
            Me.timeInSeconds = ((Me.timeInSeconds - (Me.timeInSeconds Mod ONE_MIN)) + value)
        End Set
    End Property

    REM Advance one second
    Public Sub advanceSecond(ByVal seconds As Integer)
        Me.timeInSeconds += seconds
        Me.timeInSeconds = (Me.timeInSeconds Mod MAX_TIME)
    End Sub

    REM Rollback one second
    Public Sub rollbackSecond(ByVal seconds As Integer)
        If (seconds <= Me.timeInSeconds) Then
            Me.timeInSeconds -= seconds
        Else
            seconds = (seconds Mod MAX_TIME)
            Me.timeInSeconds = (Me.timeInSeconds - seconds)
            Me.timeInSeconds = (Me.timeInSeconds + MAX_TIME)
            Me.timeInSeconds = (Me.timeInSeconds Mod MAX_TIME)
        End If
    End Sub
End Class
 
Take a look at the TimeSpan class, you might be reinventing a perfectly good wheel needlessly.

Other points:

*Using ' instead of REM for comments might make things less cluttered looking
*While we use camelCase for variable names, we generally use PascalCase for Property, Sub and Function names
*This is the VS.NET General Discussion; for general discussion about the IDE, not the language. Future topics such as this one ought to go in VB.NET Gen Diss, not VS.NET Gen Diss :)
 
You can have value returned by using ByRef instead of ByVal

if I have a sub...

VB.NET:
Sub Add(ByVal x as integer, ByVal y as integer, ByRef Total as integer)
Total = x+y
end sub

and I call it like

VB.NET:
Dim total as integer

add(x,y,total)

then total WILL have the correct value in it, because I used Byref.

But you wouldnt do that, because in OO terms, that is Not Good. Kinda like swearing in church :)

Byref changes the value of the original varible you referenced, byval makes a copy of that variable, and only that copy is changed.
:cool:

Not quite. ByVal causes a copy of the stack-pointer to be passed, ByRef causes the original pointer to be passed. At no time is the heap data of the variable duplicated. Avoid implying that:

Total = a + b

causes total to change. Actually total is initialised to a whole new different object on the heap, and the original pointer is attached to the new object. Nothing was changed, but one thing was destroyed and one thing was created.
 
Back
Top