[SOLVED] Work with array property lazarus / 64-FPC development ---------------------------------------------------------------------- Okoba · 2026-01-28 17:27 Hello, In this sample can you tell me how can I correctly get length of the array, and add or delete elements to it? I can cast it to Integer array but I like to know the generic way that works for all element types. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program Project1;{$mode objfpc}{$H+}{$M+}{$RTTI EXPLICIT   PROPERTIES([vcPrivate,vcProtected,vcPublic,vcPublished])   FIELDS([vcPrivate,vcProtected,vcPublic,vcPublished])   METHODS([vcPrivate,vcProtected,vcPublic,vcPublished])}uses  TypInfo; type  TTest = record    A: array of integer;  end;var  T: TTest;  FieldTable: PExtendedFieldInfoTable;  FieldCount, I: Integer;begin  T.A := [1, 2, 3];  FieldCount := GetFieldList(TypeInfo(TTest), FieldTable);  for I := 0 to FieldCount - 1 do    with FieldTable^[I]^ do    begin      if Name^ = 'A' then    begin      How to get the array length?      How to add another item to the array?      end;    end;  ReadLn;end. ---------------------------------------------------------------------- ALLIGATOR · 2026-01-28 18:52 It's scary, but it works Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program Project1;{$mode objfpc}{$H+}{$M+}{$RTTI EXPLICIT   PROPERTIES([vcPrivate,vcProtected,vcPublic,vcPublished])   FIELDS([vcPrivate,vcProtected,vcPublic,vcPublished])   METHODS([vcPrivate,vcProtected,vcPublic,vcPublished])}uses  TypInfo; type  TTest = record    A: array of UInt64;  end;var  T: TTest;  FieldTable: PExtendedFieldInfoTable;  FieldCount, I: Integer;  newlen: SizeInt = 33;begin  T.A := [1, 2, 3];  FieldCount := GetFieldList(TypeInfo(TTest), FieldTable);  for I := 0 to FieldCount - 1 do    if FieldTable^[I]^.Name^ = 'A' then    begin      //How to get the array length?      WriteLn(DynArrayBounds(@PByte(@T)[FieldTable^[I]^.FieldOffset], FieldTable^[I]^.FieldType^)[0]+1);       //How to add another item to the array?      DynArraySetLength(PPointer(@PByte(@T)[FieldTable^[I]^.FieldOffset])^, FieldTable^[I]^.FieldType^, 1, @newlen);    end;   WriteLn(Length(T.A));  ReadLn;end. ---------------------------------------------------------------------- Okoba · 2026-01-29 07:20 Thank you! Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i PPointer(@PByte(@T)[FieldTable^[I]^.FieldOffset])^This part is confusing? it is pointer of the array property? How can i set the newly added element dynamically depending on the array element type? like if it is integer set it to 1 and if string set to '1'? ---------------------------------------------------------------------- ALLIGATOR · 2026-01-29 08:10 Something like that, but I'm starting to dislike this code more and more Besides, I'm still confused as to why I get the array element type this way. I originally wrote it differently, but it didn't work... And this worked, but I don't have time to figure it out yet, so I'll leave it as is Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program Project1;{$mode objfpc}{$H+}{$M+}{$RTTI EXPLICIT   PROPERTIES([vcPrivate,vcProtected,vcPublic,vcPublished])   FIELDS([vcPrivate,vcProtected,vcPublic,vcPublished])   METHODS([vcPrivate,vcProtected,vcPublic,vcPublished])}uses  TypInfo; type  TTest = record    A: array of Integer;  end;var  T: TTest;  FieldTable: PExtendedFieldInfoTable;  FieldCount, I: Integer;  newlen: SizeInt = 33;  s: string;  ptd: PTypeData;begin  T.A := [1, 2, 3];  FieldCount := GetFieldList(TypeInfo(TTest), FieldTable);  for I := 0 to FieldCount - 1 do    if FieldTable^[I]^.Name^ = 'A' then    begin      //How to get the array length?      WriteLn(DynArrayBounds(@PByte(@T)[FieldTable^[I]^.FieldOffset], FieldTable^[I]^.FieldType^)[0]+1);       //How to add another item to the array?      DynArraySetLength(PPointer(@PByte(@T)[FieldTable^[I]^.FieldOffset])^, FieldTable^[I]^.FieldType^, 1, @newlen);       WriteStr(s, FieldTable^[I]^.FieldType^^.Kind);      WriteLn(s);       case FieldTable^[I]^.FieldType^^.Kind of        tkUnknown:;        tkInteger:;        tkChar:;        tkEnumeration:;        tkFloat:;      tkSet:;        tkMethod:;        tkSString:;        tkLString:;      tkAString:;        tkWString:;        tkVariant:;        tkArray:;        tkRecord:;        tkInterface:;        tkClass:;      tkObject:;        tkWChar:;        tkBool:;        tkInt64:;        tkQWord:;        tkDynArray:        begin          ptd:=GetTypeData(FieldTable^[I]^.FieldType^);           case ptd^.OrdType of            otSLong: PInteger(PPointer(@PByte(@T)[FieldTable^[I]^.FieldOffset])^)[3]:=44;        end;         end;        tkInterfaceRaw:;        tkProcVar:;      tkUString:;        tkUChar:;        tkHelper:;        tkFile:;      tkClassRef:;        tkPointer:;      end;     end;   WriteLn(Length(T.A));  WriteLn(T.A[0]);  WriteLn(T.A[1]);  WriteLn(T.A[2]);  WriteLn(T.A[3]);  WriteLn(T.A[4]);  ReadLn;end. ---------------------------------------------------------------------- PascalDragon · 2026-01-29 20:02 Quote from: Okoba on January 29, 2026, 08:20:04 amCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i PPointer(@PByte(@T)[FieldTable^[I]^.FieldOffset])^This part is confusing? it is pointer of the array property? It's a field, not a property.