How to store form method pointers in a TList (Views: 100)
What is the correct way to store form method pointers on a TList then use the TList items to call the various procedures?
This isn't as easy as it seems. Despite the name, method pointers aren't pointers, they're 8-byte records (type TMethod, check the Help). So, you have to New a TMethod on the heap for each method pointer you want to store to the list, and of course free the records before you clear or free the list:
TmyMethod = procedure(s: string) of object;
TpMyMethod = ^TmyMethod;
TpMethPtr = ^Tmethod;
procedure addMethodPointer(lst: TList; mp: TmyMethod);
p^ := Tmethod(mp);
procedure clearPointers(lst: TList);
for j := 0 to lst.count - 1 do
The fancy casting in addMethodPointer is so you can change the parameter type for mp without changing anything else.
The call to a given pointer goes this way:
<< Back to main page