RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TList.Pack Method

Deletes all nil (Delphi) or NULL (C++) items from the Items array.

Pascal
procedure Pack;
C++
__fastcall Pack();

Call Pack to move all non-nil (Delphi) or non-NULL (C++) items to the front of the Items array and reduce the Count property to the number of items actually used. Pack does not free up the memory used to store the nil (Delphi) or NULL (C++) pointers. To free up the memory for the unused entries removed by Pack, set the Capacity property to the new value of Count.  

C++ Examples: 

 

/*
This example requires two edit box controls and a button on
the form. The code creates a list object and adds some
strings to it. The forth string in the list is a null string.
The code counts the number of strings in the list and
displays the number in the Edit1 control. The code then
packs the list, removing the nil string, and counts the
strings in the list again. The second count displays in the
Edit2 control:
*/
void __fastcall  DisplayTList(TList *TheList)
{
    // Now paint the items onto the paintbox}
    short Y = 10;             // Variable used in TextOut function
    for (byte B = 0; B < TheList->Count; B++)
    {
      AnsiString str = PChar(TheList->Items[B]);
      Form1->Canvas->TextOut(10, Y, str);
      Y = Y + 30;  // Increment Y Value again
    }
}

void __fastcall TForm1::FormCreate(TObject *Sender)
{
  MyList = new TList; // Create a list
  MyList->Add(PChar("A string")); // Add a string
  MyList->Add(PChar("")); // Add an empty string
  MyList->Add(PChar("A third string")); // Add a string
  MyList->Add(NULL);              // Add nil
  MyList->Add(PChar("A fifth string")); // Add a string
  MyList->Add(PChar("")); // Add another empty string
  MyList->Add(PChar("A seventh string")); // Add a string
  Edit1->Text = IntToStr(MyList->Count); // Put count into Edit1
}

void __fastcall TForm1::Button1Click(TObject *Sender)
{
  MyList->Pack();                         // Pack the list.
  Edit2->Text = IntToStr(MyList->Count); // Put count into Edit2
  Repaint();
}

void __fastcall TForm1::FormPaint(TObject *Sender)
{
  DisplayTList(MyList);
}

void __fastcall TForm1::FormDestroy(TObject *Sender)
{
  delete MyList;                         // Free memory for list
}

 

Delphi Examples: 

{
This example requires two edit box controls and a button on
the form. The code creates a list object and adds some
strings to it. The second string in the list is a nil string.
The code counts the number of strings in the list and
displays the number in the Edit1 control. The code then
packs the list, removing the nil string, and counts the
strings in the list again. The second count displays in the
Edit2 control:
}
procedure TForm1.FormCreate(Sender: TObject);
var
  I: Integer;
begin
  MyList := TList.Create;              {Create a list }
  MyList.Add(PChar('A string')); {Add a string}
  MyList.Add(PChar('')); { Add an empty string }
  MyList.Add(PChar('A third string')); {Add a string}
  MyList.Add(nil);              {Add nil }
  MyList.Add(PChar('A fifth string')); {Add a string}
  MyList.Add(PChar('')); { Add another empty string }
  MyList.Add(PChar('A seventh string')); {Add a string}
  Edit1.Text := IntToStr(MyList.Count); {Put count into Edit1}
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  MyList.Pack;                         {Pack the list.}
  Edit2.Text := IntToStr(MyList.Count); {Put count into Edit2}
  Repaint;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  MyList.Free;                         {Free memory for list}
end;

procedure TForm1.FormPaint(Sender: TObject);
begin
  DisplayTList(MyList);
end;

 

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