Re: How to move one element of Dynamic Array of Objects
- From: Rob Kennedy <me3@xxxxxxxxxxx>
- Date: Wed, 26 Mar 2008 08:34:59 -0500
TeChNoInSiDe wrote:
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
How about here?
http://pages.cs.wisc.edu/~rkennedy/array-delete
I demonstrate two ways of removing something from an array.
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.
If that code seems to work correctly, you're not looking very carefully. Test it again, but this time, delete an element earlier than the second-to-last one.
That "SizeOf(TestArray)" term is wrong whether you're using a loop or not. I think you meant "SizeOf(TestArray[0])" instead. You're moving the things in the array, so the number of bytes should be based on the array's contents, not the size of the variable.
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]));
You don't really need the first SetLength.
//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));
What is VIv_i?
Remember that you're not really doesn't anything to TestArray. You're updating just one of its elements, TestArray[i].
Parentheses aren't required in "TestArray[(i - 1)]."
Did you know that you can assign arrays to each other?
TestArray[i - 1] := TestArray[i];
That can be the entire contents of your loop.
Do *not* use "Move" to remove an element when the element type is "dynamic array," unless you use the full version of DeleteX from the page I cite above. The Initialize and Finalize calls are crucial.
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.
--
Rob
.
- 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: Re: install delphi 4 and 6 safely on same computer
- Next by Date: Re: How to move one element of Dynamic Array of Objects
- Previous by thread: Re: 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):