Re: How to move one element of Dynamic Array of Objects
- From: "Maarten Wiltink" <maarten@xxxxxxxxxxxxxxxxxx>
- Date: Wed, 26 Mar 2008 10:29:38 +0100
"TeChNoInSiDe" <tomatinhum@xxxxxxxxx> wrote in message
news:74b6311f-5b85-4de2-83ef-fa5c1a0123c4@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
<delete object from dynamic array>
FreeAndNil(TestArray[1]);
If you're going to overwrite the array element shortly, there's
really no need to set it to nil first.
This is part of a bigger pattern where FreeAndNil is of very,
very, *very* limited use. Slightly less than goto, I'd estimate,
and about as harmful or more so.
In most cases like this, you can arrange things so the reference
to the object you just freed is going to go away in the next
line or two anyway.
//Move component at index 2 to index 1
for i := 2 to High(TestArray) do begin
System.Move(TestArray[i], TestArray[(i - 1)], SizeOf(TestArray) *
(Length(TestArray) - (i - 1) - 1));
end;
One Move will suffice.
The above code seems to be working correctly. The problem is when I
try to make the same thing with multi-dimensional arrays like this:
var TestArray : Array of Array of TPanel;
begin
//clone components
TestArray[0][0] := TPanel cloned component;
TestArray[0][1] := TPanel cloned component;
TestArray[0][2] := TPanel cloned component;
TestArray[1][0] := TPanel cloned component;
TestArray[1][1] := TPanel cloned component;
TestArray[1][2] := TPanel cloned component;
TestArray[1][3] := TPanel cloned component;
TestArray[1][4] := TPanel cloned component;
TestArray[2][0] := TPanel cloned component;
Even without reading further, I think I know what's happening.
TestArray is a dynamic array of... things; it doesn't even matter.
When deleting item 0 from it, do not further concern yourself with
the things inside items 1 and 2. Move them up one item opaquely.
The entire nested dynamic arrays in TestArray[1] and TestArray[2]
will be moved at once.
Dynamic array variables contain pointers. Everything else follows
from that.
Groetjes,
Maarten Wiltink
.
- References:
- How to move one element of Dynamic Array of Objects
- From: TeChNoInSiDe
- How to move one element of Dynamic Array of Objects
- Prev by Date: How to move one element of Dynamic Array of Objects
- Next by Date: Re: install delphi 4 and 6 safely on same computer
- Previous by thread: How to move one element of Dynamic Array of Objects
- Next by thread: Re: How to move one element of Dynamic Array of Objects
- Index(es):