How to share functions in Delphi? -


for example, have couple of functions written form. now, need exact same functions in form. so, how can share them between 2 forms? please, provide simple example if possible.

don't put them in form. separate them , put them in common unit, , add unit uses clause need access them.

here's quick example, can see many of delphi rtl units (for instance, sysutils) this. (you should learn use vcl/rtl source , demo apps included in delphi; answer many of questions you've posted more waiting answer here.)

sharedfunctions.pas:

unit    sharedfunctions;  interface  uses   sysutils;  // add other units needed  function dosomething: string;  implementation  function dosomething: string; begin   result := 'something done'; end;  end. 

unita.pas

unit   yourmainform;  uses   sysutils;  interface  type   tmainform = class(tform)        procedure formshow(sender: tobject);     // other stuff   end;  implementation  uses   sharedfunctions;  procedure tmainform.formshow(sender: tobject); begin   showmessage(dosomething()); end;  end. 

in more recent versions of delphi delphi 7, can create functions/methods in record instead:

unit   sharedfunctions;  interface  uses   sysutils;  type   tsharedfunctions = record   public     class function dosomething: string;   end;  implementation  function tsharedfunctions.dosomething: string; begin   result := 'something done'; end;  end; 

unitb.pas

unit   yourmainform;  uses   sysutils;  interface  type   tmainform = class(tform)        procedure formshow(sender: tobject);     // other stuff   end;  implementation  uses   sharedfunctions;  procedure tmainform.formshow(sender: tobject); begin   showmessage(tsharedfunctions.dosomething()); end;  end. 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -