Function Max in Visual Basic 6.0?-VBForums
Results 1 to 5 of 5

Thread: Function Max in Visual Basic 6.0?

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2007
    Posts
    36

    Function Max in Visual Basic 6.0?

    Hi there,

    I read about the function "Max" and "Min" in the newest version of VB and also in VB for Applications (Excel).

    But they don't seem to exist in VB 6.0.

    Or is there a special trick to call them?

    I would like to call these functions so that I can use them when I transfer my code to VB 2005.

    Thanks for your help.

  2. #2
    Frenzied Member
    Join Date
    Jul 2007
    Posts
    1,306

    Re: Function Max in Visual Basic 6.0?

    I think Min and Max are Excell functions, not VB or VBA functions. Min and Max functions are not hard to implement I think.
    Anyway , using excell application you can use them. eg:
    Code:
        Dim exApp As New Excel.Application
        Set exApp = CreateObject("Excel.Application")
        MsgBox exApp.WorksheetFunction.Min(20, 501, 561, 45, 78)
    IIF(Post.Rate > 0 , , )

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    37,027

    Re: Function Max in Visual Basic 6.0?

    That method would be slow, and a waste of resources (especially if you don't also close the object!)

    Of course as you said, they are not hard to implement.. Here's an example of how you could create the Max function:
    Code:
    Function MyMax(ParamArray TheValues() As Variant) As Variant
    'Return the "highest" of the values
    
    Dim intLoop As Integer
    Dim varCurrentMax As Variant
      varCurrentMax = TheValues(LBound(TheValues))
      For intLoop = LBound(TheValues) + 1 To UBound(TheValues)
        If TheValues(intLoop) > varCurrentMax Then
          varCurrentMax = TheValues(intLoop)
        End If
      Next intLoop
      
      MyMax = varCurrentMax
    
    End Function
    Example usage:
    Code:
    MsgBox MyMax(10, 20, 3, 17)
    ..note that it would be more efficient/accurate to use proper data types rather than Variants, but then you would need separate functions for each data type (or group, as Long/Int/Byte could all be covered by a single Long version).

  4. #4
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,438

    Re: Function Max in Visual Basic 6.0?

    Quote Originally Posted by si_the_geek
    Function MyMax(ParamArray TheValues() As Variant) As Variant

    [...]

    note that it would be more efficient/accurate to use proper data types rather than Variants, but then you would need separate functions for each data type (or group, as Long/Int/Byte could all be covered by a single Long version).
    I don't think you can pass arrays by value, and when you pass by reference you have to match the type. (So I don't think you could use Long for Integer and Byte.)

    Also, just a quick tip regarding your declaration. The way you wrote it is an array of variants, which is less efficient than a variant array:

    Function MyMax(ParamArray TheValues As Variant) As Variant

    The difference is the variant overhead. In a variant array, the variant overhead is only used once for the entire array. In an array of variants, the overhead is used for every element.

    While it can be confusing to declare a variant array -- since it has no tell-tale () array signifier -- it is a better way to go.

    EDIT: On second thought, I don't think this applies to parameter arrays; because each parameter can be a different type, I think it has to be an array of variants.
    Last edited by Ellis Dee; Sep 8th, 2007 at 10:14 AM.

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    37,027

    Re: Function Max in Visual Basic 6.0?

    I understand your point on the declaration, but that is actually the way you have to declare a ParamArray (your alteration creates a compile error).

    Your first comment is slightly flawed (as a ParamArray isn't the same thing as an array - it is only received as an array), but did make me realise a bigger flaw in what I had said.. I had forgotten that you can only use the Variant data type with a ParamArray, so for a typed version of the function you would need to use an Array rather than a ParamArray, and at that point your comment is correct.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.