lang • foreach

FOR EACH

FOR EACH Variable IN Expression ... NEXT

Repeats a loop while enumerating an object.

Expression must be a reference to an enumerable object: for example, a collection, or an array.

Example

Dim Dict As New Collection
Dim Element As String

Dict["Blue"] = 3
Dict["Red"] = 1
Dict["Green"] = 2

For Each Element In Dict
  Print Element;
Next
3 1 2

FOR EACH (2)

FOR EACH Expression ... NEXT

This syntax must be used when Expression is a enumerable object that is not a real container: for example, the result of a database query.

Example

Dim Res As Result

Res = DB.Exec("SELECT * FROM MyTable")

For Each Res
  Print Res!Code; " "; Res!Name
Next

The order of the enumeration in not necessarily predictable. See the documentation of each enumerable class for more details on that.

See also