Mega Code Archive

 
Categories / Delphi / Examples
 

Tlist

I've found this is a useful procedure to clean up a straight forward TList. procedure FreeList(List : TList); var eachItem : Integer; begin for eachItem := 0 to Pred(List.Count) do begin TObject(List.Items[eachItem]).Free; end; List.Clear; List.Free; end; ************************************************ PROBLEMS WITH TLIST.DELETE..? Hi Gary for future reference... a way of solving the wee problem I showed you a minute ago is to find an algorithm that starts at the TOP of the list and works downwards, as opposed to starting at index 0 and working upwards. Doing it the 'down to' way makes it easy to handle both the TList.Count property decremented within the loop and also current index values that are 'pushed downwards' every time an object is deleted... eg procedure TEntityHandler.DeleteSelectedEntities; //iterate through all entities: if an entity is //selected, then delete it from the object list var listIdx: Integer; begin listIdx := (objectList.Count - 1); while (listIdx >= 0) do begin if (TEntity(objectList.Items[listIdx]).selected = True) then begin objectList.Delete(listIdx); end; Dec(listIdx); end; end;