PDA

View Full Version : [SOLVED] StopWatch or Timer Class Module



fredlo2010
12-19-2013, 05:58 PM
Hello guys,

I created a class to act as a timer but I am getting an error 28 Out of Stack space. I thought It was because I had a nested function. I removed that and I still get the same.

This is my class code:

Option Explicit'==================================
'Class Name: xlMyStopwatch
'Class Instancing: 1-Private
'==================================

' Declare the variables for the class
Private d_Start As Date
Private d_Final As Date

' Declare the properties for the class
Public Property Get timeStart() As Date
timeStart = d_Start
End Property


Public Property Get timeEnd() As Date
timeEnd = d_Final
End Property


Public Property Let timeStart(ByVal tStart As Date)
timeStart = tStart
End Property


Public Property Let timeEnd(ByVal tEnd As Date)
timeEnd = tEnd
End Property


' Methods and Functions
' Function to get the difference of time
' My original work contained a single function


'Public Function Difference(sInterval As String)
' Difference = DateDiff(sInterval, Me.timeStart, Me.timeEnd)
'End Function




Public Function ElapsedSeconds() As Long
ElapsedSeconds = DateDiff("s", Me.timeStart, Me.timeEnd)
End Function


Public Function ElapsedMinutes() As Long
ElapsedMinutes = DateDiff("m", Me.timeStart, Me.timeEnd)
End Function


Public Function ElapsedHours() As Long
ElapsedHours = DateDiff("h", Me.timeStart, Me.timeEnd)
End Function


Public Function ElapsedTime() As String
ElapsedTime = Format(Me.timeEnd - Me.timeStart, "hh:mm:ss")
End Function

and this is a sub I created and its giving me the error

Sub Create()


Dim myStopwatch As xlMyStopwatch


Set myStopwatch = New xlMyStopwatch

myStopwatch.timeStart = Now '<========== Running out of space error 28

' A small sample of dunny code to try the class
For Each r In Range("A1:A40")
r.Value = 1
Next r

myStopwatch.timeEnd = Now

Debug.Print myStopwatch.ElapsedHours
Debug.Print myStopwatch.ElapsedMinutes
Debug.Print myStopwatch.ElapsedSeconds
Debug.Print myStopwatch.ElapsedTime

End Sub

Thanks in advance for all the help. :)

Paul_Hossler
12-19-2013, 09:41 PM
This (as well as others) is calling itself.




Public Property Let timeStart(ByVal tStart As Date)
timeStart = tStart
End Property


Was easy to spot by single stepping through Create() using F8


You really wanted to set the variable:



Public Property Let timeStart(ByVal tStart As Date)
d_Start = tStart
End Property


Paul

fredlo2010
12-20-2013, 04:37 AM
Thanks a lot Paul. It works like a charm now. I don't remember now If I was debugging it correctly with F8 last night. I tried it today and you are correct the function was calling itself in an infinite loop.

This is my final xlMYStopwatch class:

Option Explicit

'==================================
'Class Name: xlMyStopwatch
'Class Instancing: 1-Private
'==================================

' Declare the variables for the class
Private d_Start As Date
Private d_Final As Date

' Declare the properties for the class
Public Property Get timeStart() As Date
timeStart = d_Start
End Property

Public Property Get timeEnd() As Date
timeEnd = d_Final
End Property

Public Property Let timeStart(ByVal tStart As Date)
d_Start = tStart
End Property

Public Property Let timeEnd(ByVal tEnd As Date)
d_Final = tEnd
End Property

' Methods and Functions
' Function to get the difference of time
Private Function Difference(sInterval As String) As Long
Difference = DateDiff(sInterval, Me.timeStart, Me.timeEnd)
End Function

Public Function ElapsedSeconds(Optional bUnits As Boolean = False) As String
If bUnits = True Then
ElapsedSeconds = Difference("s") & " " & "seconds"
Else
ElapsedSeconds = Difference("s")
End If
End Function

Public Function ElapsedMinutes(Optional bUnits As Boolean = False) As String
If bUnits = True Then
ElapsedMinutes = Difference("m") & " " & "minutes"
Else
ElapsedMinutes = Difference("m")
End If
End Function

Public Function ElapsedHours(Optional bUnits As Boolean = False) As String
If bUnits = True Then
ElapsedHours = Difference("h") & " " & "hours"
Else
ElapsedHours = Difference("h")
End If
End Function

Public Function ElapsedTime() As String
ElapsedTime = Format(Me.timeEnd - Me.timeStart, "hh:mm:ss")
End Function