CorArray: Exists

Exists

Determines whether the specified array contains elements that match the conditions defined by the specified predicate.



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

Parameters

Arr
[ByRef] Variant. The one-dimensional array to search.
AddressOfPredicate
[ByVal] Long. The AddressOf a callback method used to determine if an element matches the specified criteria.

Return Values

Boolean -  True if Arr contains one or more elements that match the conditions defined by the specified predicate callback; otherwise, False.

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 code uses the callback Person18OrOlder with the Exists method to search an array of Longs. The callback method is passed individual values representing a persons age and returns True if the age is of an adult or False if not.

Public Sub Main()
    Dim Ages() As Long
    Dim Found As Boolean
    
    Ages = NewLongs(4, 16, 10, 22, 14, 19)
    
    Found = CorArray.Exists(Ages, AddressOf Person18OrOlder)
    
    Debug.Print "There is at least one adult: " & Found
End Sub

Private Function Person18OrOlder(ByRef Age As Long) As Boolean
    Person18OrOlder = Age >= 18
End Function

' The code example produces the following output.
'  There is at least one adult: True

See Also

Project CorLib Overview

Class CorArray Overview

Find

FindIndex

FindLast

FindLastIndex

FindAll