For in CommandOfficial Content

Allows traversing an array or a collection.

Syntax

For &Var in expression
    code
Endfor

Where:

&Var
   Specifies a variable that must have the same data type as the items of the expression

expression
   Any expression whose value is a collection, array or matrix.

code
   GeneXus code to be run in each iteration.

Notes:

  • The array/matrix/collection values cannot be changed during the scanning. This means that changes in the &Var value, in the command scope, do not affect the corresponding array/matrix/collection or vice versa.
  • The vector/matrix/collection position cannot be obtained during the scanning. For this, a variable must be defined as counter.
  • These structures can be nested to scan several arrays, matrixes or collections. This takes into account when a Subroutine that also has a For In is called within the structure.
  • Like in a For Each command (X Evolution 2) or Do While, commands can be included that "break" the scanning, such as Exit or Return.

Examples

Suppose the expression is an array variable: 

for &var in &varArray()
   … // code
endfor

The previous code stores each value of the array &varArray in the &var variable. For vectors of one dimension, the code is expanded to:

&x = 1
Do while &x <= rows(&varArray())
   &var = &varArray(&x)
   ... // code
   &x += 1
EndDo

 Expression can also be an array of two dimension (matrix) variables:

for &var in &varMatrix()
   ... // code
endfor

The expanded code would be as follows:

&x = 1
Do while &x <= rows(&varMatrix())
   &y = 1
    Do while &y <= cols(&varMatrix())
       &var = &varMatrix( &x, &y)
       ... // code
       &y += 1
    EndDo
  &x += 1
EndDo

It may even a collection variable of any type (including a variable with Collection Property set as 'False' but of an 'SDT collection' data type). For more details, see the two ways of Implementing SDT collections:

for &var in &collection
    ...
endfor

An expression can not only be a variable, but also, for example, the result of a Data Provider or a Procedure:

for &var in DataProvider(parms)
    ...
endfor
for &var in Procedure(parms)
    ...
endfor

Remember not to write any method, 'Udp method' is assumed.

In sum, any expression whose evaluation results in a collection, array or matrix, is a valid one to run through.

See Also

Collection variables
Collection Domains




Was this page helpful?
What Is This?
Your feedback about this content is important. Let us know what you think.