CorArray: TrueForAll

TrueForAll

Determines whether every element in the array matches the conditions defined by the specified callback method.



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

Parameters

Arr
[ByRef] Variant. The array to check against the conditions.
AddressOfPredicate
[ByVal] Long. The callback address used to match the criteria.

Return Values

Boolean -  True if every element in array matches the conditions defined by the specified predicate; otherwise, False. If there are no elements in the array, the return value is True.

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 the method returns False for any element.

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

ExceptionConditioni
ArgumentNullException Arr is uninitialized
RankException Arr has more than one dimension.

Examples

The following code example checks two arrays for containing only ages 18 or older.

Private Sub Main()
    Dim Adults() As Long
    Dim Mixed() As Long
        
    Adults = NewLongs(22, 45, 37, 62, 18)
    Mixed = NewLongs(29, 33, 16, 49, 13)
    
    Debug.Print "Adults array has only adults:"
    Debug.Print CorArray.TrueForAll(Adults, AddressOf Age18OrOlder)
    
    Debug.Print t("\nMixed array has only adults:")
    Debug.Print CorArray.TrueForAll(Mixed, AddressOf Age18OrOlder)
End Sub

' The method accepts a ByRef parameter of the array element type.
Private Function Age18OrOlder(ByRef Age As Long) As Boolean
    Age18OrOlder = Age >= 18
End Function

' This code example produces the following output.
'    Adults array has only adults:
'    True
'
'    Mixed array has only adults:
'    False

See Also

Project CorLib Overview

Class CorArray Overview