|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here! |
|
#1
|
|||
|
|||
|
How to selectively delete from TList
I have a snippet of code that iterates through a TList and deletes some items. The code is as follows:
Code:
procedure PurgeList;
var
P: PSomeRecord;
I: Integer;
begin
I := 0;
while (I < FList.Count) do
begin
P := PSomeRecord(FList[I]);
if P^.CanDelete then
begin
FreeMem(P);
P := nil;
FList.Delete(I);
Continue;
end;
Inc(I);
end;
end;
Is this the right way to delete items from a tlist? or will it enter into an infinite loop? can anyone show me any other way to remove items from a tlist? |
|
#2
|
|||
|
|||
|
Your code could land you in trouble.
This is better: When deleting from a list count down from the top. That way the items you have not yet visited maintain their index place in the loop. Pseudo Code: Code:
for i := FList.count-1 down to 0 do begin // if FList[i] should be deleted delete it end; Clive Last edited by clivew : April 29th, 2008 at 01:04 AM. Reason: Made code a little clearer |
|
#3
|
|||
|
|||
|
thanks for the tip. i've updated my code accordingly.
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > Delphi Programming > How to selectively delete from TList |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|