How to appropriately overload a VBA function requiring different object types for a parameter

105 Views Asked by At

Below have a function to be overloaded requiring one parameter that can be either a DateTime or DateTimeOffset object.

What would be the appropriate method to implement this overloading? i.e

GetUtcOffset(optional Byval inDateTime as DateTime, optional Byval inDateTimeOffset as DateTimeOffset) As TimeSpan

or

GetUtcOffset(ByVal inDateTimeOrDateTimeOffset As Object) As TimeSpan

Another option maybe something similar to the Factory pattern if there's a design pattern for overloading methods or pass in something like an IArguments pattern. Tending to prefer using optional parameters over this option.

Next option is to use TwinBasic I think it provides method overloading thou until it's 100% compatibile will investigate in the future.

Second question: Which would be the prefered method If statements or a Select to test the parameter?

I have a few similar scenerios and need to consider the appropriate method.

'@Description  "Calculates the offset or difference between the time in this time zone and Coordinated Universal Time (UTC) for a particular date and time."
'Overloads
'   GetUtcOffset (DateTime)
'       Calculates the offset or difference between the time in this time zone and Coordinated
'       Universal Time (UTC) for a particular date and time.
'   GetUtcOffset (DateTimeOffset)
'       Calculates the offset or difference between the time in this time zone and Coordinated
'       Universal Time (UTC) for a particular date and time.
'Parameters
'   inObjDateTime (DateTime or DateTimeOffset)
'       The date and time to determine the offset for.
'Returns TimeSpan
'   An object that indicates the time difference between the two time zones.
'@TODO How to appropriately overload?
'   GetUtcOffset(optional Byval inDateTime as DateTime, optional Byval inDateTimeOffset as DateTimeOffset)
'or GetUtcOffset(ByVal inDateTimeOrDateTimeOffset As Object) As TimeSpan
Public Function GetUtcOffset(ByVal inDateTimeOrDateTimeOffset As Object) As TimeSpan
    If TypeOf inDateTimeOrDateTimeOffset Is DateTime Then
        'Set GetUtcOffset = GetUtcOffsetFromDateTime(inDateTimeOrDateTimeOffset)
    ElseIf TypeOf inDateTimeOrDateTimeOffset Is DateTimeOffset Then
        'Set GetUtcOffset = GetUtcOffsetFromDateTimeOffset(inDateTimeOrDateTimeOffset)
    Else
        'Raise Error invalid argument
    End If

    'Or use Select ??
    Select Case True
        Case TypeOf inDateTimeOrDateTimeOffset Is DateTime
            'Set GetUtcOffset = GetUtcOffsetFromDateTime(inDateTimeOrDateTimeOffset)
        Case TypeOf inDateTimeOrDateTimeOffset Is DateTimeOffset
            'Set GetUtcOffset = GetUtcOffsetFromDateTimeOffset(inDateTimeOrDateTimeOffset)'
        Case Else
            'Raise Error invalid argument
    End Select
End Function

For second question have examined : Select Case on an object's type in VB.NET

Any extra input would be great, I'm prefering the Select Case unless overwhelming reasons to avoid.

0

There are 0 best solutions below