5 Replies - 4795 Views - Last Post: 01 August 2009 - 01:00 AM Rate Topic: -----

#1 mikjall77  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 47
  • Joined: 18-April 07

Stopwatch with split time in OOP

Posted 23 October 2007 - 01:08 PM

I designing a stopwatch type OO program and am stuck on trying to figure out the split time function of a stop watch. I want to list the split times in a list box as the user presses the split button and also display the final split time when the user presses the stop button. I feel I'm real close but just can't get over the hump, especially trying to code in OOP. The rest of my program works fine (start, stop, elapsed time). I am programming in VB 2005 and will display my relative code from both the class and form areas. Please let me know if you need more information or more code displayed.

Thank you,
Mikjall77


CLASS CODE

Public Class clsTimer
	Implements IDisposable

	'properties
	Private m_StartTime As Date
	Private m_StopTime As Date
	Private m_ElapsedTime As TimeSpan
	Private m_SplitTime As New TimeSpan
	Private m_SplitTime2 As New TimeSpan
	Private m_SplitTime3 As New TimeSpan
	Private bClockRunning As Boolean   'flag for clock state
	'exceptions
	Public ClockStartedException As New Exception("The clock has already been started")
	Public ClockStoppededException As New Exception("The clock has already been stopped")
	'events
	Public Event ClockStarted()
	Public Event ClockStopped()

Public ReadOnly Property ElapsedTime() As TimeSpan
		Get
			If bClockRunning Then
				'calculate current elapsed time
				m_ElapsedTime = Now - m_StartTime
				'alternate
				'm_ElapsedTime = Now.Subtract(m_StartTime)
			Else
				'clock is not running
				m_ElapsedTime = m_StopTime - m_StartTime
			End If
			Return m_ElapsedTime
		End Get
	End Property

'***** PROBLEM AREA FOLLOWS THIS *******

	Public ReadOnly Property SplitTime() As TimeSpan
		Get
			If m_SplitTime = Nothing Then
				m_SplitTime = Now - m_StartTime
				m_SplitTime2 = m_SplitTime
				Return m_SplitTime
			Else
				m_SplitTime2 = m_ElapsedTime - m_SplitTime2
				m_SplitTime3 = m_SplitTime2 - m_SplitTime3
				Return m_SplitTime3
			End If
		End Get
	End Property


'******PROBLEM AREA IS ABOVE THIS*********

FORM CODE

Private Sub btnSplit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSplit.Click
		Dim spnSplit As TimeSpan
		spnSplit = oTimer.SplitTime
		With spnSplit
			lstSplitTimes.Items.Add(Format(.Hours, "00") & ":" & _
			Format(.Minutes, "00") & ":" & Format(.Seconds, "00") & _
			":" & Format(.Milliseconds, "000"))
		End With
	End Sub

	Private Sub tmrElapsed_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrElapsed.Tick
		Dim spnTime As TimeSpan

		spnTime = oTimer.ElapsedTime
		With spnTime
			lblElapsed.Text = Format(.Hours, "00") & ":" & ":" & Format(.Minutes, "00") & ":" & Format(.Seconds, "00") & ":" & .Milliseconds
		End With
	End Sub







Is This A Good Question/Topic? 0
  • +

Replies To: Stopwatch with split time in OOP

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1638
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Stopwatch with split time in OOP

Posted 23 October 2007 - 01:20 PM

Are you getting an error? What is it displaying versus what you're looking for? Sorry but I'm not going to sit and compile your code to see what you're getting, you're going to have to give some explanation on that.

Thanks :)
Was This Post Helpful? 0
  • +
  • -

#3 mikjall77  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 47
  • Joined: 18-April 07

Re: Stopwatch with split time in OOP

Posted 23 October 2007 - 01:51 PM

View PostPsychoCoder, on 23 Oct, 2007 - 01:20 PM, said:

Are you getting an error? What is it displaying versus what you're looking for? Sorry but I'm not going to sit and compile your code to see what you're getting, you're going to have to give some explanation on that.

Thanks :)


Sorry that I forgot to mention my results. I'm not getting an error just that my results seem high. For the first two times that I press split, the split times seem to be correct (especially the first one). After that, it almost seems that something isn't resetting and the split times are higher than what they should be. After a test run, I got these results (trying to press the split time button every 2 seconds)
01:495
01:976
02:175
02:362
05:116
02:149
07:556
02:248
09:921
02:278
12:128
02:259
As I said, I tried to press the split button approximately every two seconds. I have a feeling that it is reading something from somewhere that it shouldn't be.

This post has been edited by mikjall77: 23 October 2007 - 02:15 PM

Was This Post Helpful? 0
  • +
  • -

#4 aceofspades686  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 6
  • View blog
  • Posts: 334
  • Joined: 08-October 07

Re: Stopwatch with split time in OOP

Posted 23 October 2007 - 02:57 PM

Well, the easiest way I see to do this would be, have a separate timer for the split time that starts with the main timer, and every time that they press the split button, return that value and reset it. That may be a simpler method than what you're attempting to do right now. I'll be honest, I'm not entirely sure that I know the exact end result you're looking for, but hopefully this was of some help.
Was This Post Helpful? 0
  • +
  • -

#5 mikjall77  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 47
  • Joined: 18-April 07

Re: Stopwatch with split time in OOP

Posted 24 October 2007 - 03:01 AM

I found my mistake and have the stopwatch working properly now. I was setting one of the items to a previous value when it should have been set to a current value. Thanks for suggestions and attempts to help me.
Was This Post Helpful? 0
  • +
  • -

#6 CheckersW  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 12
  • View blog
  • Posts: 198
  • Joined: 04-April 09

Re: Stopwatch with split time in OOP

Posted 01 August 2009 - 01:00 AM

I know this isn't exactly related to your (solved) problem, but for anyone else creating a similar program who stumbles across this forum: I recommend not having a milliseconds feature, as the demand a changed value has on the system is so great that it slows down the speed of the timer, so what your program shows as 5 seconds may actually be (in my case at least) 8 seconds.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1