CorArray: Find |
Uses a callback method to search an array for the first element that matches the criteria.
Public Function Find( ByRef Arr As Variant, ByVal AddressOfPredicate As Long ) As Variant
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.
The following example will search a given array for the first 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 first age over 25, pass the array and ' address of the callback method to CorArray.Find. Age = CorArray.Find(Ages, AddressOf AgeOver25) ' Dispay the index of the first age found. Debug.Print "The first 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 index of the first age over 25 found: 2