CorArray: FindAll |
Retrieves all the elements that match the conditions defined by the specified callback method.
Public Function FindAll( 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. All elements of Arr are individually passed to the callback method and includes in the result if they match the criteria.
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 returned array is of the same type as Arr.
The following example will search a given array for all ages that are over 25.
The result will return an array of Longs containing the matched values.
Private Sub Main() Dim Ages() As Long Dim AgesOver25() As Long Dim Age As Variant Ages = NewLongs(14, 22, 31, 27, 18) ' To find all ages over 25, pass the array and ' address of the callback method to CorArray.FindAll. AgesOver25 = CorArray.FindAll(Ages, AddressOf AgeOver25) ' Dispay all the ages found. Debug.Print "All ages over 25: " For Each Age In AgesOver25 Debug.Print Age Next 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. ' All ages over 25: ' 31 ' 27