Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

TList Programming

Posted on 1999-07-27
5
Medium Priority
?
281 Views
Last Modified: 2011-10-03
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
Comment
Question by:RBertora
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 2

Accepted Solution

by:
kotik earned 200 total points
ID: 1391430
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
 
LVL 13

Expert Comment

by:Epsylon
ID: 1391431
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
 
LVL 7

Author Comment

by:RBertora
ID: 1391432
Thats perfect ,Thanks

Rob.
0
 
LVL 2

Expert Comment

by:kotik
ID: 1391433
Welcome any time :)

0
 
LVL 7

Author Comment

by:RBertora
ID: 1391434
Epsylon, I like your solution too only read it after implementing kotik's method... though.. thanks anyway.
Rob.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question