• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 283
  • Last Modified:

TList Programming

Hi ,

I am working with a TList for the first time.. and am
screwing up wildly.....

I want a list of records of this type.

Type
  TRobBalance = record
    HolidayBalance : real;
    SickdayBalance : real;
  end;

Can someone provide me with

A procedure to init a TList structure of these records (or equivalent)
A procedure to assign values to a new record and add it to the list
A procedure to loop through all the Tlist records and add the     holidaybalance value to say a TMemo component.
A procedure to free all this stuff.

Thanks
Rob.
0
RBertora
Asked:
RBertora
  • 2
  • 2
1 Solution
 
kotikCommented:
You can create your own list class as:

TMyList = class(TList)
...
{you must implement here your methods }
end;

or you can try this:
1)
  MyList: TList;
 
  then you must create your list:
  MyList := TList.Create;
 
procedure AddItem(AItem: TRobBalance);
var P: ^TRobBalance;
begin
  new(P);
  P^ := AItem;
  MyList.Add(P);
end;

procedure LoopDemo;
var
  i: integer;
  s : string;
  item: TRobBalance;
begin
  for i:=0 to MyList.Count - 1 do begin
    item := TRobBalance(MyList.Items[i]^);
    s := Format('%s - %s', [item.HolidayBalance, item.SickdayBalance);
    Memo1.Items.Add(s);
  end;
end;

procedure ClearList;
var i: integer;
begin
  with MyList do begin
//    MyList.Pack;
    for i := 0 to Count-1  do begin
      Dispose(Items[i]);
    end;
    Clear;
  end;
end;

Does it suit you?
0
 
EpsylonCommented:
Hi Rob, there's no need to create new classes. Just use some type casting:


Type
  PRobBalance = ^TRobBalance;
  TRobBalance = record
    HolidayBalance : real;
    SickdayBalance : real;
  end;

var l: TList;
    r: PRobBalance;
    i: Integer;
begin
  l := TList.Create;
  r := AllocMem(sizeof(TRobBalance));
  r.HolidayBalance := 1.5;
  r.SickdayBalance := 4.6;
  l.Add(r);
  ShowMessage(FloatToStr(PRobBalance(l.First).HolidayBalance) +
              #13#10 +
              FloatToStr(PRobBalance(l.First).SickdayBalance));

  for i := 0 to l.Count - 1 do
    FreeMem(l.Items[i]);
  l.Free;
end;


Cheers,

Epsylon.
0
 
RBertoraAuthor Commented:
Thats perfect ,Thanks

Rob.
0
 
kotikCommented:
Welcome any time :)

0
 
RBertoraAuthor Commented:
Epsylon, I like your solution too only read it after implementing kotik's method... though.. thanks anyway.
Rob.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now