Forum > Beginners

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

<< < (2/3) > >>


--- Quote from: Nimral on May 06, 2021, 10:28:01 pm ---What is the purpose of introducing a local copy then? It doesn't make much sense to me?
--- End quote ---

Using enumerators (which is the "technical" name of that ... hmmm ... technic) in a loop is basically a strategy, or a convenient way, to read through dynamic array-like structures without the need to know or care (as would be case with e.g. a for Low(x) to High(h)) for its size or indexing. That's it.

As Remy explained, inside the loop you deal with a copy of the contained object, so write operations are valid only for that copy, not for the original.

But don't worry too much: this has been bugging out people all over the world since it was introduced ;D

Try in following way.

--- 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  PPayload = ^TPayload;   TPayload = record    ID: integer;    Name: string[50];  end;   TArrPayload = array of PPayload;  // not array of TPayload var  ArrPayload: TArrPayload;  rec: PPayLoad;                    // define rec as pointer type, not record type  // ....................  for rec in ArrPayload do     VirtualStringTree1.AddChild(nil, rec);     //  rec itself is a pointer.

BTW, I came to use VST through answering to your questions. So, do not thank me :D


welcome to the VST hell :-) 3+ Years of development by Mike Lischke alone, 2600 pages of pdf documentation, 32000+ lines of code, working with pointers, giving almost unlimited flexibility which leaves almost unlimited opportunities for hard to find glitches.

Aside from this minor obstacles VST is a great and very versatile component :-) It took me about 3 weeks to get along with it, and I am still not quite done.

Regarding your suggestion: did you test it using my little demo project linked above? I did a quick test, and it won't compile, Error "Got TPayload, expected pointer".

IMHO, like Remy and others have outlined, rec is a local copy of the enumerated record, not a pointer, and adding its address to the tree will later, when you try to access the record via the stored address, create memory havoc, since the local copy of rec is long gone by then. My test program behaved just like that, so I assume that yours won't work.

Greetings from rainy Bavaria,


If you want to store... Always use packed arrays. Problem solved..

Hm, how would a packed array help in this situation?


Btw, you seem to be the most rapidy typing person in the world ...? See attachment :-)


[0] Message Index

[#] Next page

[*] Previous page

Go to full version