I am trying to fill an array using data from a database (Access), but when I run this code:
with dmSUPREMEDATA do
begin
iNumberofRecords := ADOComplete.RecordCount;
ADOComplete.First;
SetLength(ArrSummary, iNumberofRecords, 3);
for i := 0 to iNumberofRecords do
begin
ArrSummary[i, 0] := ADOComplete['Names'];
ArrSummary[i, 1] := ADOComplete['Surnames'];
ArrSummary[i, 2] := ADOComplete['Average'];
ADOComplete.Next;
end;
end;
This is the error that pops up:
Is there a better way to fill the array? This is coded with delphi
CodePudding user response:
Your loop is going out out bounds of the array.
The 1st dimension of the array has iNumberofRecords
number of elements, so it has valid indexes 0..iNumberofRecords-1
. The indexes that a for
loop uses are inclusive, so you are looping through indexes 0..iNumberofRecords
, which means the final iteration of the loop is accessing an invalid index.
You need to subtract -1
from the loop counter, eg:
for i := 0 to iNumberofRecords-1 do
or:
for i := 0 to Pred(iNumberofRecords) do
CodePudding user response:
You can try create two for loops. Like a detail and subdetail, being one for the first array and other for the second.
var arr2d := array of array of string;
SetLength(arr2d, 5, 5);
for var i := 0 to Length(arr2d) - 1 do
begin
for var j := 0 to Length(arr2d) - 1 do
arr2d[i][j] := 'Text [' i.ToString '][' j.toString ']';
end