delphi - How to call object method for any object in my metaclass? -


i have object structure:

tjsonstructure = class(tobject);  treqbase = class(tjsonstructure) private    token: int64; public    procedure fillwithtemplatedata; virtual; end;  treqlogin = class(treqbase) private    username,    password: string;    module  : integer; public    procedure fillwithtemplatedata; override; end;  procedure treqbase.fillwithtemplatedata; begin    token := ...; end;  procedure treqlogin.fillwithtemplatedata; begin    inherited;    username := ...;    password := ...;    module   := ...; end;  type    twebact = (ttlogin,               ttsignin);  treqclass = class of treqbase;  const    cwebactstructures: array[twebact] of    record       requestclass : treqclass;    end    = (       { ttlogin  } (requestclass: treqlogin;),       { ttsignin } (requestclass: treqsignin;)     // not in definitions above      );  

now do:

var    lwebact       : twebact;    lrequestclass : treqclass; begin    lwebact := low(twebact) high(twebact)    begin       lrequestclass := cwebactstructures[lwebact].requestclass; 

and want call

lrequestclass.fillwithtemplatedata; 

in order execute treqlogin.fillwithtemplatedata when lwebact = ttlogin etc.
won't compile: e2706 form of method call allowed class methods.

i understand reason (the text of compiler message) how can fix treqlogin.fillwithtemplatedata gets executed when lwebact=ttlogin etc without having handle list of treqlogin, treqsignin types in code (again)?

lrequestclass class reference. can call class methods on it, not instance methods. , fillwithtemplatedata instance method.

you need have instance call instance method. instantiate one:

var    req: treqbase;  .... req := lrequestclass.create;  try   req.fillwithtemplatedata;   ...   req.free; end; 

if develop classes need perform work in constructors must introduce virtual constructor treqbase. , override in derived classes. that's way can make sure derived constructor runs when instantiating class reference.

perhaps system requires instances instantiated in other way, cannot tell here. no matter what, instantiate then, need instance call instance method.


Comments

Popular posts from this blog

matlab - Deleting rows with specific rules -

jquery - How would i go about shortening this code? And to cancel the previous click on click of new section? -