Skip to main content

IndexOfObject

function IndexOfObject(AObject: TObject): Integer;

Example

procedure FindContext(const Items: TStringList; const Context: TObject;
var FoundAt: Integer);
begin
FoundAt := Items.IndexOfObject(Context);
end;

Usage

IndexOfObject returns the first entry associated with the exact TObject reference, or minus one.

  • Keep associated objects alive independently when the list is non-owning.
  • Use >= 0 to test success.
  • Do not dereference an object obtained from the list unless its external lifetime is still guaranteed.

Additional Technical Info

IndexOfObject is declared by hidden ancestor TStrings and is normally used through a concrete visible descendant such as TStringList. It compares references, not class names, object state or values.

Source-backed behaviour

The base method scans from zero and tests GetObject(Index) = AObject. TStringList provides an equivalent optimized direct scan. The first identical pointer wins; duplicate associations are permitted.

Searching for nil returns the first entry with no associated object. A dangling reference previously stored in the list is still only a pointer value; this search does not validate that the target object remains alive.

Objects reads and writes associations. AddObject creates a pair. IndexOf searches text.

Official RTL references

Created 2026-07-15