The For Each Statement

Syntax

$ ForEachStatement = FOR EACH ident1 IN ident2 DO StatementSequence END FOR ";"

Description

The For Each statement is used for an enclosed statements sequence cycling for each element in the collection. The For Each cycle is executed if there is at least one element in the collection. After the cycle starts, the statements are executed for the first element of the ident2 collection and a value is inserted into the ident1 variable; if there are other elements in the collection, the statements will be executed in series for all the elements. If there are no elements, the cycle breaks and the statement, following the End For statement, takes the control.

This statement is optimized for working with a dictionary elements array provided by the IDimElementArray interface. As a result, the collection dictionary element numbers will be returned.

Example

Sub ForEachSample;
Var
    d: Double;
    Arr: Array Of Double;
    TabSheetBox1: TabSheetBox;
Begin
    Arr := TabSheetBox1.Source.GetTabSheet.ParseRange("A0:K0").ToDoubleArray(True);
    For Each d In Arr Do
        Debug.Write(d.ToString + " ");
    End For;
End Sub ForEachSample;

After executing the example the console window displays all values of non-empty cells from the range A0:K0.

Sub ForEachDimArray;
Var
    MB: IMetabase;
    DimInst: IDimInstance;
    Elem: IDimElementArray;
    i: Integer;
Begin
    MB := MetabaseClass.Active;
    DimInst := MB.ItemById("D_TO").Open(NullAs IDimInstance;
    Elem := DimInst.RootElements;
    For Each i In Elem Do
        Debug.Write(i.ToString + " ");
    End For;
End Sub ForEachDimArray;

After executing the example the console window displays all root elements indexes of the dictionary with the D_TO identifier.

See also:

Statements