RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TList.Items Property

Lists the object references.

Pascal
property Items [Index: Integer]: Pointer;
C++
__property void * Items[int Index];

Use Items to obtain a pointer to a specific object in the array. The Index parameter indicates the index of the object, where 0 is the index of the first object, 1 is the index of the second object, and so on. Set Items to change the reference at a specific location. 

Use Items with the Count property to iterate through all of the objects in the list. 

Not all of the entries in the Items array need to contain references to objects. Some of the entries may be nil (Delphi) or NULL (C++) pointers. To remove the nil (Delphi) or NULL (C++) pointers and reduce the size of the Items array to the number of objects, call the Pack method.

Note: Items is the default property for TList. This means you can omit the property name. Thus, instead of
Note: MyList.Items[i]
Note: you can write
Note: MyList[i]
 

C++ Examples: 

 

/*
This example creates a list object and inserts two records
into it. The value of the record fields are written on a
paintbox:
*/

typedef struct AList
{
  int I;
  char C;
} TAList;

typedef TAList* PAList;

void __fastcall TForm1::Button1Click(TObject *Sender)
{
  PAList AStruct;
  TList *MyList = new TList;
  // fill the TList
  AStruct = new TAList;
  AStruct->I = 100;
  AStruct->C = 'Z';
  MyList->Add(AStruct);
  AStruct = new TAList;
  AStruct->I = 200;
  AStruct->C = 'X';
  MyList->Add(AStruct);
  
  // Go through the list, writing the elements to the
  // canvas of a paintbox component.
  int Y = 10; // position on canvas
  for (int i = 0; i < MyList->Count; i++)
  {
    AStruct = (PAList) MyList->Items[i];
    PaintBox1->Canvas->TextOut(10, Y, IntToStr(AStruct->I));
    Y += 30;  // Increment Y Value again
    PaintBox1->Canvas->TextOut(10, Y, AStruct->C);
    Y += 30;  //Increment Y Value
  }

  // Clean up - must free memory for the items as well as the list
  for (int i = 0; i < MyList->Count; i++)
  {
    AStruct = (PAList) MyList->Items[i];
    delete AStruct;
  }
  delete MyList;

}

 

Delphi Examples: 

{
This example creates a list object and inserts two records
into it. The value of the record fields are written on a
paintbox:
} 
procedure TForm1.Button1Click(Sender: TObject);
type
  PMyList = ^AList;
  AList = record
    I: Integer;
    C: Char;
  end;
var
  MyList: TList;
  ARecord: PMyList;
  B: Byte;
  Y: Word;
begin
  MyList := TList.Create;
  try
    New(ARecord);
    ARecord^.I := 100;
    ARecord^.C := 'Z';
    MyList.Add(ARecord); {Add integer 100 and character Z to list}
    New(ARecord);
    ARecord^.I := 200;
    ARecord^.C := 'X';
    MyList.Add(ARecord); {Add integer 200 and character X to list}

    { Now paint the items onto the paintbox}
    Y := 10;             {Variable used in TextOut function}
    for B := 0 to (MyList.Count - 1) do
    begin
      ARecord := MyList.Items[B];
      Canvas.TextOut(10, Y, IntToStr(ARecord^.I)); {Display I}
      Y := Y + 30;  {Increment Y Value again}
      Canvas.TextOut(10, Y, ARecord^.C);  {Display C}
      Y := Y + 30;  {Increment Y Value}
    end;

    { Cleanup: must free the list items as well as the list }
   for B := 0 to (MyList.Count - 1) do
   begin
     ARecord := MyList.Items[B];
     Dispose(ARecord);
   end;
  finally
    MyList.Free;
  end;
end;

 

Count 

List 

Pack 

operator_sb

Copyright(C) 2008 CodeGear(TM). All Rights Reserved.
What do you think about this topic? Send feedback!