B
B
booomheadshot2014-03-22 14:33:55
Delphi
booomheadshot, 2014-03-22 14:33:55

How to dynamically expand an array?

Hello.
There is the following

the code
TEat = record
  EatName: string;
  EatCount: integer;
end;

....

var
  Eat: Array of TEat;

....

function AddEat(var Eat: TEat; EatName: string; EatCount: integer;): string;
begin
  SetLength(Eat,Length(Eat)+1);
  Eat[Length(Eat)].EatName:=Eatname;
  Eat[Length(eat)].EatCount:=EatCount;
        result:='Создан юнит еды '+Eat[length(eat)].EanName+' в количестве '+Eat[Length(eat)].EatCount+' штук.';
end;

Which, as we see, when calling the AddEat function, theoretically, should expand the Eat array by exactly one element and add parameters to it that are passed to the AddEat function. But apparently, I'm doing something wrong because it does not work. Please help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
BlueRay, 2014-03-22
@booomheadshot

If in Delphi array indexing is from zero (I don’t remember, suddenly from one), then the last element is accessed not through Eat[Length(Eat)], but through Eat[Length(Eat) - 1].

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question