[Gambas-user] Re: Time Help
njmurphy1 at ...734...
njmurphy1 at ...734...
Mon Jun 13 20:17:52 CEST 2005
Hey Murphy I'm a Murphy too.
There are as many answers to your question as there are programmers.
Here is how I would do it.
I make a form with a timer, a label and an instance of a class named
TimeFunctions, here's the code for the form:
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
' Gambas class file
PUBLIC Clock AS TimeFunctions
PUBLIC SUB Form_Open()
'make a instance of TimeFunctions named Clock
Clock = NEW TimeFunctions
'Set the initial time values in Clock
Clock.init_time()
'Put the time in the label
'now so we don't have to wait
'for the timer to fire.
textlabel1.text = Clock.Time()
END
PUBLIC SUB Timer1_Timer()
'Timer delay is set to 1000
'so timer fires every second
Clock.add_one_to_seconds()
'update textlabel1 with the time
textlabel1.text = Clock.Time()
END
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Here is the code for the TimeFunctions class:
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
' Gambas class file
'All variables are private because
'they are only used in the class.
'Should do this with oop anyway.
PRIVATE hours AS Integer
PRIVATE minutes AS Integer
PRIVATE seconds AS Integer
PUBLIC SUB init_time()
'This sets initial time.
'It would be better to set to
'the current time but
'this is an example.
hours = 12
minutes = 58
seconds = 57
END
PUBLIC FUNCTION time() AS String
'This function returns a formated string of
'the time.
RETURN hours & ":" & minutes & ":" & seconds
END
PUBLIC SUB add_one_to_seconds()
seconds = seconds + 1
'we need to check if seconds is > 59
'if so call add_one_to_minutes
'and reset seconds.
IF seconds > 59 THEN
seconds = 0
add_one_to_minutes()
ENDIF
END
PRIVATE SUB add_one_to_minutes()
minutes = minutes + 1
'we need to check if minutes is > 59
'if so call add_one_to_hours
'and reset minutes.
IF minutes > 59 THEN
minutes = 0
add_one_to_hours()
ENDIF
END
PRIVATE SUB add_one_to_hours()
hours = hours + 1
'Here we make a choice on using
'24 hour clock or 12 hour clock.
'This example uses a 12 hour.
IF hours > 12 THEN
hours = 1
ENDIF
END
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
This should give you an idea of using oop to do
what you want.
Joseph Murphy
More information about the User
mailing list