Search code examples
arraysvb.netmaxdisplay

Getting a max value from an array, where there could be multiple max values


I have made an array of ages from a database and wish the find the maximum values in the array. I have made this code so far...

    Dim MaxAge, MaxIndex, MaxCount As Integer

    MaxAge = arrAge(1)
    MaxIndex = 1

    For MaxCount = 2 To Length
        If MaxAge < arrAge(MaxCount) Then
            MaxAge = arrAge(MaxCount)
            MaxIndex = MaxCount
        End If
    Next

    lstDisplayMax.Items.Add(arrAge(MaxIndex))

This code works, however, If there are two or more maxes in the array, it will still only result in one of the maximum values being displayed. I need it to display all the max values. Thank in advance :)


Solution

  • I have found the answer. All you have to do is create a filter for the database using the arrAge(maxindex). It will display all the records in the database that share the max age.

    The code goes like this:

    Dim MaxAge, MaxIndex, MaxCount As Integer
    Dim Length As Integer = Me.listName.Items.Count - 1
    
    MaxAge = arrAge(1)
    MaxIndex = 1
    
    For MaxCount = 2 To Length
        If MaxAge < arrAge(MaxCount) Then
           MaxAge = arrAge(MaxCount)
           MaxIndex = MaxCount
        End If
    Next
    
    Me.PatientListingBindingSource.Filter = "pAge = '" & arrAge(MaxIndex) & "' "