Forum > Beginners

[Res.] Why is enumerating a dynamic array different from accessing it by Index?

<< < (3/3)


--- Quote from: Nimral on May 07, 2021, 09:49:21 am ---Btw, you seem to be the most rapidy typing person in the world ...? See attachment :-)

--- End quote ---
I think you are confusing decimal and thousand separators.

I have modified some of your codes, and it seems to work.

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---type    // ............    TArrPayload = array of PPayload;  // As I told, set this to array of pointers, not records     // ....... var    Form1: TForm1;    ArrPayload: TArrPayload;  implementation  procedure TForm1.FormCreate(Sender: TObject);const  NumItems = 5; var   i: integer;   rec: PPayload; begin    // populate a dynamic array    // ArrPayload := TArrPayload.Create;    SetLength(ArrPayLoad, NumItems);   // I fixed length, but your approach would be nicer if the size is not known.      for i := 0 to High(ArrPayLoad) do    begin        New(rec);                 // You need to allocate memory to rec with New        rec^.ID := i;        rec^.Name := Format('Node %d', [i]);         ArrPayLoad[i] := rec;      //  rec, which is pointer is stored in the array.    end;     // initalize the nodes - want the adress of the records as payloads    VirtualStringTree1.NodeDataSize := SizeOf(PPayload);     {   // I commented out these     // these work ...,       for i := low(ArrPayload) to high(ArrPayload) do     begin            VirtualStringTree1.AddChild(nil,@ArrPayload[i]);    end;     //  }   //  following seems to work   for rec in ArrPayload do       VirtualStringTree1.AddChild(nil, rec); end; procedure TForm1.FormDestroy(Sender: TObject);var     rec: PPayLoad;begin     for rec in ArrPayLoad do Dispose(rec);  // You have to free memory, as these are using heap memory, not stack. end;  

The point of packed arrays is that you can store it at once. No iterations....

I can't see how this behaviour is related to my problem. Would you please clarify?



--- Quote from: Nimral on May 07, 2021, 09:49:21 am ---Hm, how would a packed array help in this situation?

--- End quote ---

They don't. Thaddy just provides usual nonsense help... ::)

As others already mentioned: best use the ordinary for-loop, the for … in-loop is not really designed for this.


[0] Message Index

[*] Previous page

Go to full version