Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Min function

Status
Not open for further replies.

kameta

IS-IT--Management
Jan 17, 2001
16
KE
My background is foxpro where it is easy to find a minimum of a set of values. I would like to know how this is possible in VB:

I have 3 variables out of which I want to determine the one holding the minimum value. In foxpro, there is an inbuilt fuction min(var1,var2, var3). Is there a similar function in VB?
 
''Debug.Print Min(13, 12, 11, 3, 44)
Private Function Min(ParamArray Values())
Dim i As Long

Min = Values(0)
For i = 0 To UBound(Values)
If Values(i) < Min Then Min = Values(i)
Next
End Function
 
Not that I know of.
If your are dealing with 3 variables:
-------------------------------------------------
if var1<var2 then
if var1<var3 then MinVar=var1 else MinVar=var3
else
if var2<var3 then MinVar=var2 else MinVar=var3
end if
-------------------------------------------------

If you hold the values in an array -loop thought it and compare the values to the lowest found.
Sunaj
'The gap between theory and practice is not as wide in theory as it is in practice'
 
This probably isn't much of a help, but you could make your own Minimal function. Just put all the numbers in a array and cicle through the array, comparing the lowest value found with the next value.
 
Here is a little swap procedure you could use if you get into using more than a couple of values

Private arrValues() As Variant

Private Sub Swap()
Dim iX As Long
Dim iY As Long
Dim varSwap As Variant

iY = UBound(arrValues)
For iX = 0 To (iY - 1)
If arrValues(iX + 1) < arrValues(iX) Then
varSwap = arrValues(iX + 1)
arrValues(iX + 1) = arrValues(iX)
arrValues(iX) = varSwap
iX = -1
End If
Next

End Sub

You will need to load the array with the values first. May not be the best way but it works. Hope it helps. Anything is possible, the problem is I only have one lifetime.[thumbsup2]
 
Thanks folks. I now get it. I love the array method so very much.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top