Search Results for

    Show / Hide Table of Contents

    Adding a procedure to the script

    To add a procedure/function to a script, perform the following steps:

    • Create a method handler - function of the TfsCallMethodEvent type.
    • Call TfsScript.AddMethod method. The first parameter is a function syntax, the second is a link to the handler of TfsCallMethodEvent type.
    { the function itself }
    procedure TForm1.DelphiFunc(s: String; i: Integer); 
    begin 
      ShowMessage(s + ', ' + IntToStr(i)); 
    end; 
    
    { the method handler }
    function TForm1.CallMethod(Instance: TObject; ClassType: TClass; const MethodName: String; 
      var Params: Variant): Variant; 
    begin 
      DelphiFunc(Params[0], Params[1]); 
    end; 
    
    procedure TForm1.Button1Click(Sender: TObject);
    begin
      { clear all items }
      fsScript1.Clear;
      { script text }
      fsScript1.Lines := Memo1.Lines;
      { frGlobalUnit contains standard types and functions }
      fsScript1.Parent := fsGlobalUnit;
      { make DelphiFunc procedure visible to a script }
      fsScript1.AddMethod('procedure DelphiFunc(s: String; i: Integer)', CallMethod);
    
      { compile the script }
      if fsScript1.Compile then
        fsScript1.Execute else   { execute if compilation was succesfull }
        ShowMessage(fsScript1.ErrorMsg); { show an error message }
    end;
    

    If you want to add several methods, you can do it using one method handler:

      Prog.AddMethod('procedure DelphiFunc(s: String; i: Integer)', CallMethod); 
      Prog.AddMethod('procedure DelphiFunc2(s: String)', CallMethod); 
    
    { the method handler }
    function TForm1.CallMethod(Instance: TObject; ClassType: TClass; const MethodName: String; 
      var Params: Variant): Variant; 
    begin 
      { dispatch the method call } 
      if MethodName = 'DELPHIFUNC' then 
        DelphiFunc(Params[0], Params[1]) 
      else if MethodName = 'DELPHIFUNC2' then 
        DelphiFunc2(Params[0]); 
    end; 
    
    Back to top © Copyright Fast Reports Inc.