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.

The loop Variable must be a local variable or an argument of the current function.

Loop variable declaration

Seit 3.12

Loop variable can be declared directly by following that syntax:

FOR EACH Variable AS Datatype IN Expression ... NEXT

The scope of the loop variable is still function-wide. In other words, declaring the loop variable that way has the same effect as declaring it at the beginning of the function, except that you can only use it after the declaration.

You can declare the same loop variable as much time as you want provided that the declaration is the same.

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

Or

DIM Dict AS NEW Collection

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

FOR EACH Element As String 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