CorArray: ForEach |
Performs the specified action on each element of the specified array.
Public Sub ForEach( ByRef Arr As Variant, ByVal AddressOfAction As Long )
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.
Exception | Condition |
---|---|
ArgumentNullException | Arr is uninitialized. |
RankException | Arr has more than one dimension. |
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