CorArray: FindLastIndex

FindLastIndex

Finds the index of the last occurence of a matched element in the array.



 Public Function FindLastIndex(
	  ByRef Arr As Variant,
	  ByVal AddressOfPredicate As Long,
	  Optional ByRef Index As Variant,
	  Optional ByRef Count As Variant ) As Long

Parameters

Arr
[ByRef] Variant. The one-dimensional array to search.
AddressOfPredicate
[ByVal] Long. The callback address of the matching criteria method.
Index
[ByRef] Optional. Variant. The starting index in the array to begin the backward search.
Count
[ByRef] Optional. Variant. The number of elements to search.

Return Values

Long -  The index of the last occurence of the matched element.

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.

Exceptions

ExceptionCondition
ArgumentNullExceptionArr is uninitialized.
ArgumentOutOfRangeException StartIndex is outside the range of valid indexes for Arr.
-or-
Count is less than zero.
-or-
StartIndex and Count do not specify a valid section in Arr.

Examples

The following example will search a given array for the last age that is over 25 and return the index.

Private Sub Main()
    Dim Ages() As Long
    Dim Index As Long
    
    Ages = NewLongs(14, 22, 31, 27, 18)
    
    ' To find the index of the last age over 25, pass the array and
    ' address of the callback method to CorArray.FindLastIndex.
    Index = CorArray.FindLastIndex(Ages, AddressOf AgeOver25)
    
    ' Dispay the index found.
    Debug.Print "The last age over 25 found at index " & Index
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 at index 3

See Also

Project CorLib Overview

Class CorArray Overview

Exists

Find

FindIndex

FindLast

FindAll