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

TList.Add and iterating

edited December 2012 in General
Hello,

Please ,could you give me some advise:

I'm trying to make a loop to iterate trough
variables (objects) and trying to use TList
to store pointers to objects.

Here is my basic example ( RAP ):

var
i: Integer;
myList: TList;

Var1: TppVariable;
Var2: TppVariable;
Var3: TppVariable;

begin

myList := TList.Create;

myList.Add(Var1);
myList.Add(Var2);
myList.Add(Var3);

for i := 0 to myList.Count -1 do
begin

if myList.Items[i] is TppVariable then
begin
TppVariable(myList.Items[i].Value) := 'test123'; {PROBLEM IS HERE}
end;
end;

myList.Free;
end; {END}


This code works without errors but the problem is that in RAP language
that TList.Add seems to make a new instance of parameter and does not
store the pointer/reference. So, I cannot "see" the real variable
(Var1,Var2,Var3 etc.) in that loop.
That situation works correctly with Delphi TList, but not with RAP.

Thanks

Gunnar

Comments

  • edited December 2012
    Hi Gunnar,

    You need to instantiate each variable object before using them. Your
    current code contains three empty (nil) objects. Also if you plan to
    view these variables on a report, you will need to assign their Band
    property.

    ...
    begin

    myList := TList.Create;

    Var1 := TppVariable.Create(nil);
    Var2 := TppVariable.Create(nil);
    Var3 := TppVariable.Create(nil);

    Var1.Band := Report.DetailBand;
    Var2.Band := Report.DetailBand;
    Var3.Band := Report.DetailBand;

    ...

    //Don't need to free variables if Band prop is assigned.
    //Var1.Free;
    //Var2.Free;
    //Var3.Free;

    myList.Free;

    end;




    Best Regards,

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

    Thanks for the example, but still
    there is something I am doing wrong probably. Here is the
    real situation with the real variable in Report SummaryBeforePrint event:

    procedure SummaryBeforePrint;
    var
    VarList: TList;
    begin

    {Following is the real variable, designed in summary band}
    myRealVariable.Value := 'First value'; {this works OK of course}

    {now into TList}
    VarList := TList.Create;
    VarList.Add(myRealVariable);
    TppVariable(VarList.Items[0]).Value := 'Other value'; {still no luck}

    end;

    Still nothing, myRealVariable shows 'First value' on printout.

    Gunnar




  • edited December 2012
    Hi Gunnar,

    My first suggestion would be to get this working in Delphi first, then
    move the code to RAP. This way you can trace the variable value and
    event timing to find issues.

    Variable values should not be calculated inside events other than the
    OnCalc event. If you are simply trying to alter some text for the
    summary band, I suggest using a label.

    http://www.digital-metaphors.com/rbWiki/Delphi_Code/Calculations/Overview

    Best Regards,

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