(VB) When a Shared DateTime object just isn't good enough!



  • I'm not sure what this is even used for.  There are clsTime 'instances' all over the place, but never really used. 

     

    Public Class clsTime
        Private Shared mInstance As clsTime
        Private Shared mBeginTime As Date
        Private Shared mEndTime As Date

        'private new constructor
        Private Sub New()
         Diagnostics.Debug.Write("Creating clsTime singleton at: " & Now.ToLongTimeString & vbCrLf)
        End Sub

        'public function for creating/referencing clsTime object
        Public Shared Function GetTimeInstance() As clsTime
            If mInstance Is Nothing Then
                mInstance = New clsTime
            Else
                Diagnostics.Debug.Write("clsTime singleton requested at: " & Now.ToLongTimeString & vbCrLf)
            End If
            Return mInstance
        End Function

        Public Sub SetTimeInterval(ByVal BeginTime As Date, ByVal EndTime As Date)
            mBeginTime = BeginTime
            mEndTime = EndTime
        End Sub

        Public Shared Property BeginTime() As Date
            Get
                Return mBeginTime
            End Get
            Set(ByVal Value As Date)
                mBeginTime = Value
            End Set
        End Property

        Public Shared Property EndTime() As Date
            Get
                Return mEndTime
            End Get
            Set(ByVal Value As Date)
                mEndTime = Value
            End Set
        End Property

    End Class



  • looks like it is for benchmarking processes.

    They want a singleton because they don't want the overhead of creating this object all the time.  They can call it at the beginning of a process and at the end and get a fairly accurate measure of the time the process took.

     
    The problem is that it is a singleton class so if you want to measure two processes simultaneously you are pretty much screwed.
     



  • Kind of a ridiculous idea for benchmarking if that's truly what it's for.  It doesn't do enough logging by itself to actually measure anything, so anything that calls it would have to make at least two calls, in which case the caller might as well just make the same two calls to Debug.Write - which would actually be a lot less overhead than referencing another class, even a pre-existing instance of one.<hints id="hah_hints"></hints>

    Bonus points for the non-thread-safe singleton implementation and nauseating Bad Hungarian notation.  Actually, I wonder if the debug code is there not because the class is used for debugging, but because they were running into problems with thread safety.



  • Shew!  for a second there I thought EndTime was a method!



  • Cool. Looks like a classic case of the Never-Use-Public-Fields syndrome.

    [code]
    Public Module UnusedTime
     Public BeginTime As Date
     Public EndTime As Date
    End Module
    [/code]

    On a side note, why does everyone put "VB" in the caption? Does that look like an excuse? 8=]


Log in to reply