CorArray: ForEach

ForEach

Performs the specified action on each element of the specified array.



 Public Sub ForEach(
	  ByRef Arr As Variant,
	  ByVal AddressOfAction As Long )

Parameters

Arr
[ByRef] Variant. The one-dimensional array containing the elements to be processed.
AddressOfAction
[ByVal] Long. The callback address of the function to process an array element.

Remarks

The AddressOfAction is an address to a method that will perform an action on the value passed to it.

The callback method should have a signature resembling the following:

 Public Sub CallbackMethod(ByRef Value As <Type>)
   ' Perform action using Value
 End Sub
 

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
ArgumentNullException Arr is uninitialized.
RankException Arr has more than one dimension.

Examples

The following example shows how to use ForEach to display the squares of each element in an integer array.

Private Sub Main()
    Dim IntArray() As Integer
    
    IntArray = NewIntegers(2, 3, 4)
    
    CorArray.ForEach IntArray, AddressOf ShowSquares
End Sub

Private Sub ShowSquares(ByRef Value As Integer)
    Debug.Print CorString.Format("{0} squared = {1}", Value, Value * Value)
End Sub

' This code example produces the following output.
'   2 squared = 4
'   3 squared = 9
'   4 squared = 16

See Also

Project CorLib Overview

Class CorArray Overview