Theory. Accessing collection items

 


While describing the document register records, you have encountered a 1C:Enterprise script object that is a collection. It is used in the loop that processes the tabular sections of the GoodsReceipt and Services documents.

Many of the 1C:Enterprise script objects are collections. A collection is a set of objects. There are some general principles that apply to collection operations.

First, you can access each collection object by iterating through the collection items in a loop using the following syntax: For Each … In … Do … (listing 6.3).

Listing 6.3. Iterating through collection items in a loop

For Each TabularSectionRow In TabularSection Do
    Message(TabularSectionRow.MaterialOrService);
EndDo;

In this example TabularSection is a collection of tabular section rows of a configuration object. In each iteration, the TabularSectionRow variable contains the next row of the collection.

Second, you can access a collection item directly, without iterating through all items in a loop. These two approaches can be combined.

1. Some collections are named. These are the collections where each item has a unique name, and you can access an item by its name (listing 6.4).

Listing 6.4. Accessing a collection item

Catalogs.Employees;
// -OR-
Catalogs["Employees"];

In this example Catalogs is a collection of managers of all catalogs available in the configuration. Since each catalog in a configuration has a unique name, a specific item of this collection (a manager of a specific catalog) can be referenced by the catalog name: Catalogs["Employees"].

2. If naming the collection items makes no sense (the collection is not named), you can access a collection item by its index (the index for the first item in a collection is zero, listing 6.5).

Listing 6.5. Accessing a collection item by index

TabularSection[0];

In this example TabularSection is a collection of tabular section rows of a configuration object. The first item in the collection is accessed by its index 0.

Note that there are collections where both access methods are combined. For example, you can access a collection of columns in a value table either by column name or by index.


Comments
0
Add comment