CorArray: FindLast

FindLast

Searches for an element that matches the conditions defined by the specified callback method, and returns the last occurrence within the entire array.



 Public Function FindLast(
	  ByRef Arr As Variant,
	  ByVal AddressOfPredicate As Long ) As Variant

Parameters

Arr
[ByRef] Variant. The array to search.
AddressOfPredicate
[ByVal] Long. A callback address of the matching criteria method.

Return Values

Variant -  The last element in the array to match the criteria, or the default for the array type if no elements matched.

Remarks

The AddressOfPredicate is an address to a method that returns True if the value passed to it matches the conditions defined in the callback method. The elements of Arr are individually passed to the callback method, and processing is stopped when a match is found.

The callback method should have a signature resembling the following:

 Public Function CallbackMethod(ByRef Value As <Type>) As Boolean
   ' Evaluate value
 End Function
 

It has one parameter that is defined as ByRef and should be the same type as the elements in the array to be searched.

It is extremely important to define the callback method correctly. If the method is incorrect, the application may crash.

Examples

The following example will search a given array for the last occurrence of age that is over 25.

The result does not return the position of the found element, only the element itself.

Private Sub Main()
    Dim Ages() As Long
    Dim Age As Long
    
    Ages = NewLongs(14, 22, 31, 27, 18)
    
    ' To find the last age over 25, pass the array and
    ' address of the callback method to CorArray.FindLast.
    Age = CorArray.FindLast(Ages, AddressOf AgeOver25)
    
    ' Dispay the the last age over 25 found.
    Debug.Print "The last age over 25 found: " & Age
End Sub

' The method accepts a ByRef parameter of the array element type.
Private Function AgeOver25(ByRef Age As Long) As Boolean
    AgeOver25 = Age > 25
End Function

' This code example produces the following output.
'    The last age over 25 found: 27

See Also

Project CorLib Overview

Class CorArray Overview

Exists

Find

FindIndex

FindLastIndex

FindAll