Home RAP
New Blog Posts: Merging Reports - Part 1 and Part 2

How to create a global procedure / function?

edited January 2011 in RAP
Hello,
I've read the article "How To...Create RAP Globals in Code" and have two
questions:

1) How can I create a global procedure?
The article describes how to declare global variables and to assign them in
the RAP OnGlobalCreate event only - nothing for procedures / functions.

2) How can I check, if a certain global variable / global procedure is
already declared, so I don't do it twice?

Regards,
Mark

Comments

  • edited January 2011
    Hi Mark,

    1. Very similar to creating a global var. I posted this example to the
    RBWiki article as well.

    procedure TForm1.CreateRAPCode;
    var
    lCodeModule: TraCodeModule;
    lGlobalCreateProgram: TraProgram;
    begin

    lCodeModule := raGetCodeModule(ppReport1);

    lGlobalCreateProgram := lCodeModule.GlobalCreateProgram;

    if (lGlobalCreateProgram = nil) then
    begin
    lGlobalCreateProgram := TraProgram.Create;

    lGlobalCreateProgram.ProgramName := 'GlobalOnCreate';
    lGlobalCreateProgram.ChildType := ctCreateProgram;
    lGlobalCreateProgram.ProgramType := ttProcedure;
    end;

    lGlobalCreateProgram.SourceLines.Clear;
    lGlobalCreateProgram.SourceLines.Add('procedure GlobalOnCreate; ');
    lGlobalCreateProgram.SourceLines.Add('var');
    lGlobalCreateProgram.SourceLines.Add(' ldNewData: Double;');
    lGlobalCreateProgram.SourceLines.Add('begin');
    lGlobalCreateProgram.SourceLines.Add(' ldNewData := 13.0;');
    lGlobalCreateProgram.SourceLines.Add('
    Showmessage(FloatToStr(ldNewData));');
    lGlobalCreateProgram.SourceLines.Add('end;');

    lCodeModule.GlobalCreateProgram := lGlobalCreateProgram;

    lCodeModule.DSExclude([pppcEmpty]);

    {compile the code}
    lCodeModule.BuildAll(True);

    end;

    2. Use the TraCodeModule.GlobalPrograms property to determine what has
    already been created.



    Regards,

    Nico Cizik
    Digital Metaphors
    http://www.digital-metaphors.com


    Best Regards,

    Nico Cizik
    Digital Metaphors
    http://www.digital-metaphors.com
  • edited February 2011
    Hi Nico,

    thank you. Since it's a bit diffirent with a normal GlobalProgram, I was
    thinking about adding that example to the Wiki article. You have posted the
    one about OnCreate for the variable, which was already there ;)

    As long as I'm not wrong, for a "normal" global procedure it would be (note
    the 3 differences):

    -------------------------------------------
    var
    lGlobalProgram: TraProgram;
    begin
    if lCodeModule.GlobalProgramCount > 0 then begin
    for i := 0 to lCodeModule.GlobalProgramCount - 1 do begin
    if lCodeModule.GlobalPrograms[i].ProgramName = 'GlobalProcedureXYZ'
    then begin
    lGlobalProgram := lCodeModule.GlobalPrograms[i]; <- difference 1
    Break;
    end;
    end;
    end;

    if (lGlobalProgram = nil) then begin
    lGlobalProgram := TraProgram.Create;
    lGlobalProgram.ChildType := ctGlobalProgram; <- difference 2
    lGlobalProgram.ProgramName := 'GlobalProcedureXYZ';
    lGlobalProgram.ProgramType := ttProcedure;
    lCodeModule.AddProgram(lGlobalProgram); <- difference 3
    end;

    lGlobalProgram.SourceLines.Clear;
    lGlobalProgram.SourceLines.Add('procedure GlobalProcedureXYZ; ');
    lGlobalProgram.SourceLines.Add('begin');
    lGlobalProgram.SourceLines.Add(' some code');
    lGlobalProgram.SourceLines.Add('end;');

    lCodeModule.BuildAll(True);
    end;
    -------------------------------------------

    One question I still have:
    what does

    lCodeModule.DSExclude([pppcEmpty]);

    do? Is it necessary to use when creating global procedures?

    Kind regards,
    Mark


  • edited February 2011
    Hi Mark,

    The DSExclude call removes the pppcEmpty enumerated type from the design
    state of the code module. This is necessary if this is the first procedure
    created in RAP. In your case, since the procedure is already created, it
    should not matter but will not hurt anything.



    Regards,

    Nico Cizik
    Digital Metaphors
    http://www.digital-metaphors.com

    Best Regards,

    Nico Cizik
    Digital Metaphors
    http://www.digital-metaphors.com
This discussion has been closed.