If I declare a dynamic array like Astr: array of string[100], then I need to use SetLength while I don't know how many Length yet, so it do not meet my need. And if I use Tstringlist it's good for String, but not for Longint or other type.
Do you know why generics is invented? To cover case like this:
uses
fgl;
...
type
TLongIntList = specialize TFPGList<LongInt>;
TStringList = specialize TFPGList<String>;
...
var
LL: TLongIntList;
SL: TStringList;
i: Integer;
begin
LL := TLongIntList.Create;
SL := TStringList.Create;
try
LL.Add(1);
LL.Add(2);
LL.Add(3);
LL.Add(4);
SL.Add('this');
SL.Add('is');
SL.Add('my');
SL.Add('list');
for i := 0 to LL.Count - 1 begin
WriteLn(LL[i]);
end;
for i := 0 to SL.Count - 1 begin
WriteLn(SL[i]);
end;
end;
If I use similar codes above, I can create a dynamic array (without use SetLength) of any type.
What do you think?
As I've explained, it's wrong. You're overwriting some other memory region. New() allocates only as much as the pointed type.