Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2020-05

get class of procedure variable

4 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Ryan Joseph

In the example below "callback" is assigned to a method of "obj", but is it possible to get the class back from the variable? In the pseudo code I would expect .GetClass to return "obj". I think "of object" must be a record which keep both the class and method pointer but I don't know how to access it.

  type TCallback = procedure of object;
  var
    callback: TCallback;
    obj: TObject;
  begin
    callback := obj.SomeMethod;
    // get the class which callback is assigned to
    if callback.GetClass = XXX then
      ;

Regards,
Ryan Joseph

Michael Van Canneyt
to Ryan Joseph
Ryan Joseph wrote 12 lines
In the example below "callback" is assigned to a method of "obj", but is it possible to get the class back from the variable? In the pseudo code I would expect .GetClass to return "obj". I think "of object" must be a record which keep both the class and method pointer but I don't know how to access it. type TCallback = procedure of object; var callback: TCallback; obj: TObject; begin callback := obj.SomeMethod; // get the class which callback is assigned to if callback.GetClass = XXX then

I think what you're looking for is:

if TMethod(CallBack).Data=XXX then

Michael.

Michael Van Canneyt
to Michael Van Canneyt
Michael Van Canneyt wrote 23 lines
On Fri, 29 May 2020, Ryan Joseph via fpc-pascal wrote: > In the example below "callback" is assigned to a method of "obj", but is it possible to get the class back from the variable? In the pseudo code I would expect .GetClass to return "obj". I think "of object" must be a record which keep both the class and method pointer but I don't know how to access it. > > > type TCallback = procedure of object; > var > callback: TCallback; > obj: TObject; > begin > callback := obj.SomeMethod; > > // get the class which callback is assigned to > if callback.GetClass = XXX then I think what you're looking for is: if TMethod(CallBack).Data=XXX then

Small addendum, if you're looking for the class (i.e. the type) then that would be:

   if TObject(TMethod(CallBack).Data).Classtype=XXX then

TMethod is part of the system unit.
https://www.freepascal.org/docs-html/rtl/system/tmethod.html

Michael.