List objects are one-based
Properties
PC0004
Error
Design
Code Fix
Ignore Obsolete
Developers coming from C#, JavaScript, or most other languages expect zero-based indexing. In AL, List is one-based: valid indices run from 1 to Count().
Lists are 1-based indexed, that is, the indexing of a List begins with 1.
— List Data Type on Microsoft Learn
Passing 0 to Get or starting a for loop at 0 compiles without errors but raises an index-out-of-range error at runtime. Start indexing from 1.
Example
procedure ProcessList()
var
MyList: List of [Integer];
i: Integer;
begin
MyList.Add(10);
MyList.Add(20);
i := MyList.Get(0); // List objects are one-based [PC0004]
end;Use one-based indexing:
procedure ProcessList()
var
MyList: List of [Integer];
i: Integer;
begin
MyList.Add(10);
MyList.Add(20);
i := MyList.Get(1);
end;The diagnostic is also raised when a for loop iterating over a List starts at 0:
procedure IterateList()
var
MyList: List of [Text];
i: Integer;
begin
MyList.Add('First');
MyList.Add('Second');
for i := 0 to MyList.Count() do // List objects are one-based [PC0004]
Message(MyList.Get(i));
end;Start the loop at 1:
procedure IterateList()
var
MyList: List of [Text];
i: Integer;
begin
MyList.Add('First');
MyList.Add('Second');
for i := 1 to MyList.Count() do
Message(MyList.Get(i));
end;