How to store form method pointers in a TList (Views: 708)
Problem/Question/Abstract: What is the correct way to store form method pointers on a TList then use the TList items[] to call the various procedures? Answer: 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: type TmyMethod = procedure(s: string) of object; TpMyMethod = ^TmyMethod; TpMethPtr = ^Tmethod; procedure addMethodPointer(lst: TList; mp: TmyMethod); var p: TpMethPtr; begin new(p); p^ := Tmethod(mp); lst.add(p); end; procedure clearPointers(lst: TList); var j: integer; begin for j := 0 to lst.count - 1 do begin dispose(TpMethPtr(lst[j])); end; end; 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: TpMyMethod(lst[1])^(myString); |