cArray: FindLast

FindLast

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



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

Parameters

Arr
[ByRef] Variant. The array to search.
AddressOfMatch
[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 function signature for the callback has a specific format.

 Public Function FindLastCallback(ByRef x As [Array Datatype]) As Boolean
     ' return True if x matches your criteria.
 End Sub
 
The [Array Datatype] must be replaced with the datatype of the array. If the array is an array of Variants, then [Array Datatype] would be a Variant, not any specific sub-type within the variants of the array.

The callback method signature needs to have 1 parameter that is of aByRef definition for the the datatype the same as the array. The callbackfunction must return a Boolean value indicating if the passed-by-refelement matches the criteria of the matching routine.

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

Example

This example demonstrates some of the Findmethods and how to define a Callback method that the Find methodsrely on in finding array elements that match a criteria. The criteria logicis defined by the developer. The callback function is used to check if aspecific array element meets the criteria being searched for.

In the examples below, a String array is searched using variouscriteria. The two callback functions (MatchGreetings, MatchNonGreetings) aredefined with 1 parameter of a ByRef String and returns a Boolean. If the array datatype is a Long, the the callback will be defined as aByRef Long.

None of the values should be changed during the search.

Private Sub Main()    FindFirstGreeting    FindLastGreeting    FindAllGreetings    FindAllNonGreetingsEnd Sub' Searches from the beginning of the arry until the first'' successful match from the MatchGreetings function,'' returning the index the element was found at.Private Sub FindFirstGreeting()    Debug.Print cArray.FindIndex(GetStrings, AddressOf MatchGreetings)End Sub'' Searches from the end of the array until the first'' successful match from the MatchGreetings function,'' returning the index the element was found at.Private Sub FindLastGreeting()    Debug.Print cArray.FindLastIndex(GetStrings, AddressOf MatchGreetings)End Sub'' Finds all of the array elements that pass the criteria of the'' MatchGreetings function. The found elements are placed'' into a new array of the same datatype as the original array'' and returned.Private Sub FindAllGreetings()    Dim a() As String    a = cArray.FindAll(GetStrings, AddressOf MatchGreetings)    Debug.Print a(0), a(1)End Sub'' Finds all of the array elements that pass the criteria of the'' MatchNonGreetings function. The found elements are placed'' into a new array of the same datatype as the original array'' and returned.Private Sub FindAllNonGreetings()    Dim i As Long    Dim a() As String        a = cArray.FindAll(GetStrings, AddressOf MatchNonGreetings)    For i = 0 To UBound(a)        Debug.Print a(i),    Next iEnd Sub'' Create a sample array of strings to search.Private Function GetStrings() As String()    GetStrings = cArray.NewArray(ciString, "One", "Two", "Hello", "Four", "Hi", "Six")End Function'' Callback method used to match a string element from'' the array to the possible greetings we are searching for.'' The method signature should be a ByRef of the datatype'' of the specific array being searched.Public Function MatchGreetings(ByRef s As String) As Boolean    MatchGreetings = ((s = "Hello") Or (s = "Hi"))End Function'' Callback method used to match a string element from'' the array to determine that we have matched against a non-greeting.'' The method signature should be a ByRef of the datatype'' of the specific array being searched.Public Function MatchNonGreetings(ByRef s As String) As Boolean    MatchNonGreetings = Not ((s = "Hello") Or (s = "Hi"))End Function'' this code produces the following output.'''' 2'' 4'' Hello         Hi'' One           Two           Four          Six

See Also

Project VBCorLib Overview Class cArray Overview cArray Properties cArray Methods FindIndex FindLastIndex