List Data Type

Represents a strongly typed list of objects that can be accessed by index.

The following methods are available on instances of the List data type.

Method name Description
Count() Gets the number of elements contained in the List.
Add(T) Adds a value to the end of the List.
AddRange(T [, T,...]) Adds the elements of the specified collection to the end of the list.
AddRange(List of [T]) Adds the elements of the specified collection to the end of the list.
Get(Integer, var T) Gets the element at the specified index.
Get(Integer) Gets the element at the specified index. This method will raise an error if the index is outside the valid range.
Set(Integer, T) Sets the element at the specified index.
Set(Integer, T, var T) Sets the element at the specified index.
Contains(T) Determines whether an element is in the List.
IndexOf(T) Searches for the specified value and returns the one-based index of the first occurrence within the entire List.
Insert(Integer, T) Inserts an element into the List at the specified index.
LastIndexOf(T) Searches for the specified value and returns the one-based index of the last occurrence within the entire List.
Remove(T) Removes the first occurrence of a specified value from the List.
RemoveAt(Integer) Removes the element at the specified index of the List.
RemoveRange(Integer, Integer) Removes a range of elements from the List.
GetRange(Integer, Integer) Get a shallow copy of a range of elements in the source.
GetRange(Integer, Integer, var List of [T]) Get a shallow copy of a range of elements in the source.
Reverse() Reverses the order of the elements in the entire List.

Remarks

The List can only be used with simple types i.e. you can have a List of [Integer] but cannot have a List of [Blob].

Lists are 1-based indexed, that is, the indexing of a List begins with 1.

Example

In the following example, the variable CustomerNames is a list of Text values which represent customer names. The procedure WorkWithListOfCustomers displays how one would work with the List data type. The Add method is used to add the string 'John' to the CustomerNames list. The Contains method is used to check whether the list contains the specified value, in this case, the string 'John'. We continue by using the Message procedure to display a relevant message.

procedure WorkWithListOfCustomers();
var
    customerNames : List of [Text];
begin
    // Adding an element to the list
    customerNames.Add('John');

    // Checking if the list contains an element
    if customerNames.Contains('John') then
        Message('John is in the list')
    else 
        Message('John is not in the list')
end;

See Also

Getting Started with AL
Developing Extensions