How to move one element of Dynamic Array of Objects



Hi!

I read a topic about moving array indexes here:

http://groups.google.com.br/group/comp.lang.pascal.delphi.misc/browse_thread/thread/b2a8c7320a060999/8de55d44c9827f7d?hl=pt-BR&lnk=gst

The real problem is those methods aren´t working for array of objects.

For example:

===================================================

var

TestArray: Array of TAdvGlowButton;

begin

//Clone components

TestArray[0] := TAdvGlowButton component; //component cloned from
original button
TestArray[1] := TAdvGlowButton component; //component cloned from
original button
TestArray[2] := TAdvGlowButton component; //component cloned from
original button

//Delete component at index 1

FreeAndNil(TestArray[1]);

//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;

===================================================

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;

//Delete components from index 1

for i := 0 to High(TestArray[1]) do FreeAndNil(TestArray[1][i]);

//Copy all elements from index 2 to index 1

for i := 2 to High(TestArray) do
begin

//Reset and resize main index with size of next index

SetLength(TestArray[(i - 1)], 0);
SetLength(TestArray[(i - 1)], Length(TestArray[i]));

//Move data

for j := Low(TestArray[i]) to High(TestArray[i]) do
begin

System.Move(TestArray[i][j], TestArray[(i - 1)][j], SizeOf(TestArray)
* (Length(TestArray) - (VIv_i - 1) - 1));

end;

end;

This code will either generate delayed runtime access violations or
when the main application is closed it generates access violation
errors or invalid pointer operations.

Any tips?

Thank you in advance for any help.
.


Quantcast