Today we will learn three types of collections supported by AL.
A collection is a complex type that contains multiple values in one variable.
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
We will discuss about each type of collections in this post with examples.
Let’s start with most familiar collection we have used with old versions of Navision too, yes you are right, I am talking about Arrays.
Arrays
Arrays are complex variables that contain a group of values with the same data type.
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.
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;
Unlike other programming languages array index don’t starts with 0 rather with 1. In above example first element will be 1 and last 10.
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;
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 cannot 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 has some methods that are used frequently. The methods that are available for a List data type will discuss in a later post.
To create a list, use the following code:
CustomerNames: List of [Text];
To access an element in a list, use the following methods:
To store/add values to list
CustomerNames.Add(‘KSD Consultancy’);
CustomerNames.Add(‘Microsoft India’);
CustomerNames.Add(‘Ashwini Tripathi’);
To retrive values from list
CustomerNames.Get(1);
Dictionary
The Dictionary 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.
Blow code sequence will give you idea how to use dictionary data type:
//Declaring List
CustomerNamesIN: List of [Text];
CustomerNamesUS: List of [Text];
CustomerNamesCA: List of [Text];
//Declaring Dictionary
CountryWiseCustomer: Dictionary of [Code[20], List of [Text]];
//Assigning values to List
CustomerNamesIN.Add(‘KSD Consultancy’);
CustomerNamesIN.Add(‘Microsoft India’);
CustomerNamesIn.Add(‘Ashwini Tripathi’);
CustomerNamesUS.Add(‘Paul’);
CustomerNamesUS.Add(‘Linda’);
CustomerNamesCA.Add(‘Eddy’);
CustomerNamesCA.Add(‘Mark’);
//Assigning values to Dictionary
CountryWiseCustomer.Add(‘IN’,CustomerNamesIN);
CountryWiseCustomer.Add(‘US’,CustomerNamesUS);
CountryWiseCustomer.Add(‘CA’,CustomerNamesCA);
//Retrieving value from Dictionary
CountryWiseCustomer.Get(‘IN’).Get(1);
Here is the complete code:
Created new codeunit and declared variables & procedures to manipulate values in Collections.


Added Code to call procedures defined in codeunit, to assign and retrieve values from collections.

Now its time to check output of above code.

Hope you get idea how to work with Collections, you may find more posts in coming days where we may discuss about methods available for collection.
You must be logged in to post a comment.