RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TList.Last Method

Returns Items[Count - 1].

Pascal
function Last: Pointer;
C++
__fastcall void * Last();

Call Last to retrieve the last pointer in the Items array.  

C++ Examples: 

 

/*
This example inserts two records into a list object and
displays the contents of the last record in the list on the form:
*/

#include <memory>       //for STL auto_ptr class

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

typedef AList* PAList;

TList *MyList;
PAList ARecord1, ARecord2;

void __fastcall TForm1::Button1Click(TObject *Sender)
{
  PAList ARecord = reinterpret_cast<AList *>(MyList->Last());
  PaintBox1->Canvas->TextOut(10, 10, IntToStr(ARecord->I)); // Display I
  PaintBox1->Canvas->TextOut(10, 40, ARecord->C); // Display C
}

void __fastcall TForm1::Button2Click(TObject *Sender)
{
  Refresh();
}

void __fastcall TForm1::FormCreate(TObject *Sender)
{
  static std::auto_ptr<TList> _MyListCleaner(MyList = new TList);
  static std::auto_ptr<TAList> _ARecord1Cleaner(ARecord1 = new TAList);
  ARecord1->I = 100;
  ARecord1->C = 'Z';
  MyList->Add(ARecord1);
  static std::auto_ptr<TAList> _ARecord2Cleaner(ARecord2 = new TAList);
  ARecord2->I = 200;
  ARecord2->C = 'X';
  MyList->Add(ARecord2);
}

 

Delphi Examples: 

{
This example inserts two records into a list object and
displays the contents of the last record in the list on the form:
}
procedure TForm1.Button1Click(Sender: TObject);
type
  PMyList = ^AList;
  AList = record
    I: Integer;
    C: Char;
  end;
var
  MyList: TList;
  ARecord: PMyList;
  I: integer;
begin
  MyList := TList.Create;
  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}
  ARecord := MyList.Last;
  Canvas.TextOut(10, 10, IntToStr(ARecord^.I)); {Display I}
  Canvas.TextOut(10, 40, ARecord^.C);  {Display C}
  { Cleanup: must free the list items as well as the list }
  for I := 0 to (MyList.Count - 1) do
  begin
    ARecord := MyList.Items[I];
    Dispose(ARecord);
  end;
  MyList.Free;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  Refresh;
end;

 

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!