Search code examples
delphitobjectlisttlist

extract an object from a TObjectList


I have a TObjectList with OwnsObjects = true. It contains quite a few objects. Now I want to remove the object at index Idx from that list, without freeing it.

Is the Extract method the only option?

ExtractedObject := TheList.Extract(TheList[Idx]);

All other methods seem to free the object. I am looking for something a little bit more efficient, that does not do a linear search every time, since I already know the index of the object. Something like an overloaded ...

ExtractedObject := TheList.Extract(Idx);

... which does not exist.


Solution

  • Why not just set OwnsObjects to false, do your removal, then set it to true again?