For Each...Next
From Real Software Documentation
Loops through the elements of a one-dimensional array.
Syntax
For Each element [As datatype] In array
Next
| Part | Type | Description |
|---|---|---|
| element | Same type as array | A variable of the same data type as array that refers to an element of array. The loop processes each value in array. |
| datatype Introduced 2005r1
| Any valid datatype | Optional: The datatype of the array element.
It can be any one-dimensional array. If you declare the datatype with the optional AS clause, you do not have to do so with a Dim statement. The datatype must match array's datatype. |
| array | Any valid datatype | A one-dimensional array. |
Notes
The user should not make any assumptions of the traversal order as it is subject to change in the future.
You can declare variables inside the For Each loop using the Dim statement. However, such variables are local to the For Each statement and go out of scope after the last execution of the loop. For example, following loop does not compile,
For Each element as Double In values
Dim i as Double sum=sum+element
Next
i=sum+10 //i is out of scope
Examples
The following function adds up the numbers in the array passed to it.
Dim sum as Double
For Each element as Double In values
sum=sum+element
Next
Return Sum
To call the function, pass an array of doubles in a statement such as:
where s is declared as a Double and MyNumbers is an array of Doubles.
