Home All Groups Group Topic Archive Search About

How to handle data types in custom Max/Min function?

Author
9 Oct 2005 3:49 AM
LurfysMa
I would like to write a custom Max function to return the largest of
several values.

The syntax would be something like:

  result = MyMax(p1,p2,p3)

I wrote the function, which is very simple, but I can't figure out the
best way to handle the data types.

I would like it to work on any data type or even on a mix of data
types. The ideas I came up with are:

1. Use variant for everthing:

Public Function MyMax(ByRef p1, ByRef p2, ByRef p3)
MyMax = p1
If p1 > MyMax Then MyMax = p2
If p3 > MyMax Then MyMax = p3
End Function

2. I thought of adding a "type" parameter. I would take in any input
(as variant), then convert it to the speficied type. But I have to
declare the function as some type and that's what the result will be,
right?

Are there better ways? I don't really want to write a set of Max
functions like MyMaxI (integer), MyMaxD (double), etc.


--
For email, use Usenet-20031220@spamex.com

Author
9 Oct 2005 4:38 AM
Someone
Try ParamArray which allows you to make variable number of parameters. Check
the following example. It doesn't handle input with all negative numbers
because MaxValue is initially 0.

Private Function MyMax(ParamArray p() As Variant) As Variant
    Dim FirstSubType As Long
    Dim MaxValue As Variant
    Dim i As Long

    FirstSubType = VarType(p(LBound(p)))
    ' Verify that all have the same variant sub type
    For i = LBound(p) To UBound(p)
        If FirstSubType <> VarType(p(i)) Then
            MsgBox "Invalid parameters"
            Exit Function
        End If
        Debug.Print p(i)
    Next

    ' Find the maximum value.
    For i = LBound(p) To UBound(p)
        If MaxValue < p(i) Then
            MaxValue = p(i)
        End If
    Next

    MyMax = MaxValue
End Function



Show quoteHide quote
"LurfysMa" <invalid@invalid.invalid> wrote in message
news:h14hk1lj9e93mdembav4sr675ontrmb69q@4ax.com...
>I would like to write a custom Max function to return the largest of
> several values.
>
> The syntax would be something like:
>
>  result = MyMax(p1,p2,p3)
>
> I wrote the function, which is very simple, but I can't figure out the
> best way to handle the data types.
>
> I would like it to work on any data type or even on a mix of data
> types. The ideas I came up with are:
>
> 1. Use variant for everthing:
>
> Public Function MyMax(ByRef p1, ByRef p2, ByRef p3)
> MyMax = p1
> If p1 > MyMax Then MyMax = p2
> If p3 > MyMax Then MyMax = p3
> End Function
>
> 2. I thought of adding a "type" parameter. I would take in any input
> (as variant), then convert it to the speficied type. But I have to
> declare the function as some type and that's what the result will be,
> right?
>
> Are there better ways? I don't really want to write a set of Max
> functions like MyMaxI (integer), MyMaxD (double), etc.
>
>
> --
> For email, use Usenet-20031220@spamex.com
Author
9 Oct 2005 4:56 AM
LurfysMa
Show quote Hide quote
On Sun, 9 Oct 2005 00:38:37 -0400, "Someone" <nob***@cox.net> wrote:

>Try ParamArray which allows you to make variable number of parameters. Check
>the following example. It doesn't handle input with all negative numbers
>because MaxValue is initially 0.
>
>Private Function MyMax(ParamArray p() As Variant) As Variant
>    Dim FirstSubType As Long
>    Dim MaxValue As Variant
>    Dim i As Long
>
>    FirstSubType = VarType(p(LBound(p)))
>    ' Verify that all have the same variant sub type
>    For i = LBound(p) To UBound(p)
>        If FirstSubType <> VarType(p(i)) Then
>            MsgBox "Invalid parameters"
>            Exit Function
>        End If
>        Debug.Print p(i)
>    Next
>
>    ' Find the maximum value.
>    For i = LBound(p) To UBound(p)
>        If MaxValue < p(i) Then
>            MaxValue = p(i)
>        End If
>    Next
>
>    MyMax = MaxValue
>End Function

Thanks for the tip on ParamArray.

Are you suggesting that I should prohibit mixed data types? I was
hoping to be able to accept any numeric datatype, but that probably
means that the result will have to be Variant. Right?


How about this version? It makes the compiler enforce at least 2
parameters and the logic is very simple.


Public Function MyMax(vP1, vP2, ParamArray vParms())

Dim pi As Long    'Paramarray index

If vP1 > vP2 Then MyMax = vP1 Else MyMax = vP2
For pi = LBound(vParms) + 2 To UBound(vParms)
  If MyMax < vParms(pi) Then MyMax = vParms(pi)
Next pi

End Function


--
For email, use Usenet-20031220@spamex.com
Author
9 Oct 2005 5:11 AM
LurfysMa
On Sat, 08 Oct 2005 21:56:07 -0700, LurfysMa <invalid@invalid.invalid>
wrote:

Show quoteHide quote
>On Sun, 9 Oct 2005 00:38:37 -0400, "Someone" <nob***@cox.net> wrote:
>
>>Try ParamArray which allows you to make variable number of parameters. Check
>>the following example. It doesn't handle input with all negative numbers
>>because MaxValue is initially 0.
>>
>>Private Function MyMax(ParamArray p() As Variant) As Variant
>>    Dim FirstSubType As Long
>>    Dim MaxValue As Variant
>>    Dim i As Long
>>
>>    FirstSubType = VarType(p(LBound(p)))
>>    ' Verify that all have the same variant sub type
>>    For i = LBound(p) To UBound(p)
>>        If FirstSubType <> VarType(p(i)) Then
>>            MsgBox "Invalid parameters"
>>            Exit Function
>>        End If
>>        Debug.Print p(i)
>>    Next
>>
>>    ' Find the maximum value.
>>    For i = LBound(p) To UBound(p)
>>        If MaxValue < p(i) Then
>>            MaxValue = p(i)
>>        End If
>>    Next
>>
>>    MyMax = MaxValue
>>End Function
>
>Thanks for the tip on ParamArray.
>
>Are you suggesting that I should prohibit mixed data types? I was
>hoping to be able to accept any numeric datatype, but that probably
>means that the result will have to be Variant. Right?
>
>
>How about this version? It makes the compiler enforce at least 2
>parameters and the logic is very simple.
>
>
>Public Function MyMax(vP1, vP2, ParamArray vParms())
>
>Dim pi As Long    'Paramarray index
>
>If vP1 > vP2 Then MyMax = vP1 Else MyMax = vP2
>For pi = LBound(vParms) + 2 To UBound(vParms)
>  If MyMax < vParms(pi) Then MyMax = vParms(pi)
>Next pi
>
>End Function


Ooopppsss. I pasted a broken function. This is the correct one:

Public Function MyMax(vP1, vP2, ParamArray vParms())

Dim pi As Long    'Paramarray index

If vP1 > vP2 Then MyMax = vP1 Else MyMax = vP2
For pi = LBound(vParms) To UBound(vParms)
  If MyMax < vParms(pi) Then MyMax = vParms(pi)
Next pi

End Function


--
For email, use Usenet-20031220@spamex.com
Author
9 Oct 2005 5:17 AM
Someone
> Are you suggesting that I should prohibit mixed data types?

You don't have to. I would suggest that you use CDbl() to convert to Double,
because it can convert from string and other data types to double, which can
be stored in Double or Variant. In the sample, I assumed the user could call
the function with numbers and non-numbers, so you don't have to use that
check.

> For pi = LBound(vParms) + 2 To UBound(vParms)

Remove "+ 2", you could get subscript is out of range error if you use very
few parameters. I have never used ParamArray before, but knew it was there.
You may also want to check TypeName Function, which returns a string
representing the data type, you may find it useful for debugging.



Show quoteHide quote
"LurfysMa" <invalid@invalid.invalid> wrote in message
news:cd8hk11o9b7dcbhq79gth4cfjtd32f96pe@4ax.com...
> On Sun, 9 Oct 2005 00:38:37 -0400, "Someone" <nob***@cox.net> wrote:
>
>>Try ParamArray which allows you to make variable number of parameters.
>>Check
>>the following example. It doesn't handle input with all negative numbers
>>because MaxValue is initially 0.
>>
>>Private Function MyMax(ParamArray p() As Variant) As Variant
>>    Dim FirstSubType As Long
>>    Dim MaxValue As Variant
>>    Dim i As Long
>>
>>    FirstSubType = VarType(p(LBound(p)))
>>    ' Verify that all have the same variant sub type
>>    For i = LBound(p) To UBound(p)
>>        If FirstSubType <> VarType(p(i)) Then
>>            MsgBox "Invalid parameters"
>>            Exit Function
>>        End If
>>        Debug.Print p(i)
>>    Next
>>
>>    ' Find the maximum value.
>>    For i = LBound(p) To UBound(p)
>>        If MaxValue < p(i) Then
>>            MaxValue = p(i)
>>        End If
>>    Next
>>
>>    MyMax = MaxValue
>>End Function
>
> Thanks for the tip on ParamArray.
>
> Are you suggesting that I should prohibit mixed data types? I was
> hoping to be able to accept any numeric datatype, but that probably
> means that the result will have to be Variant. Right?
>
>
> How about this version? It makes the compiler enforce at least 2
> parameters and the logic is very simple.
>
>
> Public Function MyMax(vP1, vP2, ParamArray vParms())
>
> Dim pi As Long    'Paramarray index
>
> If vP1 > vP2 Then MyMax = vP1 Else MyMax = vP2
> For pi = LBound(vParms) + 2 To UBound(vParms)
>  If MyMax < vParms(pi) Then MyMax = vParms(pi)
> Next pi
>
> End Function
>
>
> --
> For email, use Usenet-20031220@spamex.com