Delphi passing Types in parameters -


an example, need solve problem in framework developing :

//unit2 :     procedure a(aform : tform; aclasstype: tformclass);      begin         showmessage (aclasstype(aform).edtuser.text);     end;       ...  //unit1 :      uses unit2;      begin        form1 := tform1.create;        try             a(form1, tform1);                   form1.free;        end;     end; 

the compiler not accept line:

aclasstype (aform).edtuser.text 

one solution use:

uses     unitoftform1;  procedure (aform: tform; aclasstype: tform1); begin     showmessage (aclasstype (aform).edtuser.text); end; 

but can not because giving circular reference , need decoupling in framework

is there way make typecast passing parameter type of form or there way ?

what asking can done either:

  1. deriving various form classes common base class exposes field want access:

    procedure a(aform : tbaseform);  begin    showmessage(aform.edtuser.text); end;  

    type   tbaseform = class(tform)     edtuser: tedit;     ...   end;    tderivedform = class(tbaseform)     ...   end;  ...  frm := tderivedform.create; try     a(frm);    frm.free; end; 
  2. or, use generics:

    type   tbaseform = class(tform)     edtuser: tedit;     ...   end;    tmyclass = class     class procedure a<t: tbaseform>(aform : t);    end;  class procedure tmyclass.a<t>(aform : t);  begin     showmessage(aform.edtuser.text); end;  

    frm := tderivedform.create; try     a<tderivedform>(frm);    frm.free; end; 
  3. if common base class not possible, use interface instead:

    type   imyformaccess = interface     function getusertext: string;   end;  procedure a(aform : imyformaccess);  begin    showmessage(aform.getusertext); end;  

    type   tform1 = class(tform, imyformaccess)     ...     function getusertext: string;     ...   end;  function tform1.getusertext: string; begin   result := edtuser.text; end;  ...  frm := tform1.create; try     a(frm imyformaccess);   frm.free; end; 
  4. or, use legacy rtti (only works published properties):

    uses   typinfo;  procedure a(aform : tform);  var   edt: tedit; begin    edt := tedit(getobjectprop(aform, 'edtuser', tedit));   if edt <> nil     showmessage(edt.text); end;  

    frm := tform1.create; try     a(frm);   frm.free; end; 
  5. or, use extended rtti (works fields , visibility):

    uses   system.rtti;  procedure a(aform : tform);  var   ctx: trtticontext;   fld: trttifield;   value: tvalue;   edt: tedit; begin    fld := ctx.gettype(aform.classtype).getfield('edtuser');   if fld <> nil   begin     value := fld.getvalue(aform);     if (not value.isempty) , value.isobject     begin       edt := value.asobject tedit;       showmessage(edt.text);     end;   end; end; 

    frm := tform1.create; try     a(frm);   frm.free; end; 

Comments

Popular posts from this blog

javascript - oscilloscope of speaker input stops rendering after a few seconds -

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -