Search code examples
delphicustom-controlsdelphi-7tlist

Delphi 7 TListSortCompare that can access object properties


I'm writing a custom component that owns a Tlist of records . the problem is : how the TListSortCompare function -used to sort the list's records- can access component's fields ? the compiler refuses object method as a list compareator , and in the component's unit there is no instance created yet to access .

Thanks Wael


Solution

  • The compare function can't be a non-static class method, it has to be either a standalone function or a static class method, which means it has no Self parameter to directly access any component object, it only knows about the 2 input parameters that point to the records being compared.

    So, the only ways for you to indirectly access the component object inside your compare function is if you either:

    • store a pointer to the component object in a global or threadvar variable.

    • store a pointer to the component object inside the records themselves.

    • use a thunk for the comparer, where the pointer to the component object is stored hidden inside the thunk itself (this is the technique the VCL uses internally to allow Win32 HWND message handlers to call TWinControl.WindowProc on a per-object basis).