Work with collections

Completed

AL supports three types of collections. A collection is a complex type that contains multiple values in one variable. All of those collections are strongly typed, which means that you can't have values with different types in the same collection. For example, you can't add date values in a collection that only allows integer values. The three types of collections that AL supports are:

  • Array

  • List

  • Dictionary

Arrays

Arrays are complex variables that contain a group of values with the same data type. An array is declared as a variable with the following traits:

  • An identifier

  • A data type

  • One or more elements

  • An index

  • A dimension

An array holds multiple values, and these values are stored in the elements of the array. You can access these values by using the index, which can also be a value that is stored in another variable. With this design, you can create a loop where you increment a certain variable to loop through every element in an array. By using the Dimension property, you can define how many dimensions that the array will hold.

When creating a variable of an array data type, you first need to define how many elements that you'll have in the array. The most commonly used array is the one-dimensional array, which is a list of elements with the same data type.

You can represent an array as a row of values, as shown in the following figure.

Example showing a One-dimensional array.

To create an array, use the following code:

SalesAmount: array[10] of Integer;

To access an element in an array, use the array element syntax:

SalesAmount[5] := 0;

In the preceding example, SalesAmount is the identifier, and you are accessing the fifth element in the array. You are setting the value of zero (0) in the fifth element. In AL, you start counting at one (1) for an array, while other programming languages, like Java, C#, and so on, usually start counting at zero (0) for the first element of an array.

Having only one element between the square brackets indicates that you are using a one-dimensional array. If you want to have a multi-dimensional array, use a comma-separated list between the brackets, as follows:

SalesAmount: array[6,9] of Integer;

To access an element in an array, use the array element syntax:

SalesAmount[5,3] := 0;

A multi-dimensional array can be represented as a table of values, as shown in the following figure.

Example showing a Multi-dimensional array.

Lists

The List data type can be compared with an array. The List type can only be used with fundamental types and represents a strongly typed list of values that can be accessed by index.

Therefore, you can have a List type of [Integer], but you can't have a List type of [Blob].

A List data type doesn't require you to define how many elements you want to store up front (while an Array data type does).

The List data type uses the System.Collections.Generic.List<T> Class from the .NET Framework, which enables you to use some built-in methods.

The List data type has some methods that are used frequently. The methods that are available for a List data type are discussed in a later module of this learning path; however, the following figure shows how to create a new variable with a type of List.

Example displaying a basic List data type.

Dictionary

The Dictionary data type uses the System.Collections.Generic.Dictionary<TKey, TValue> Class from the .NET Framework, which enables you to use some built-in methods. This data type represents a collection of keys and values.

Every key that you create in this dictionary must be unique. The main benefit is that you can immediately get the value for a specific key.

The value can be a type, but it can also be a List or another Dictionary data type.

Example displaying a Dictionary data type.