简体   繁体   English

Delphi 7 TListSortCompare 可以访问对象属性

[英]Delphi 7 TListSortCompare that can access object properties

I'm writing a custom component that owns a Tlist of records .我正在编写一个拥有记录 Tlist 的自定义组件。 the problem is : how the TListSortCompare function -used to sort the list's records- can access component's fields ?问题是:TListSortCompare 函数(用于对列表的记录进行排序)如何访问组件的字段? 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谢谢威尔

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. compare 函数不能是非静态类方法,它必须是独立函数或静态类方法,这意味着它没有Self参数可以直接访问任何组件对象,它只知道 2 个输入参数指向正在比较的记录。

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.将指向组件对象的指针存储在全局变量或threadvar变量中。

  • 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).为比较器使用 thunk,其中指向组件对象的指针隐藏在 thunk 内部存储(这是 VCL 在内部使用的技术,以允许 Win32 HWND 消息处理程序在每个对象的基础上调用TWinControl.WindowProc )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM