I'll show in this article, a little more about Delphi. This time we'll see how we can make a list of objects, using the TList and creating a class. In our example, let's start by creating a class TCliente, for this we need a new unit, rename for Cliente.pas and save. We'll only create the basic methods of list, such as Count, Add and Remove.
(If you want to learn more about Delphi, go to our Delphi online courses)
unit Cliente; interface implementation end.
Now let's prepare our class. Note that I declared the Cliente class, inheriting from TObject. After this, I declared the class properties, IDCliente, Nome,CNPJ, this time I'll press "CTRL + Shift + C" to that Delphi can prepare my methods gets and sets.
unit Cliente;
interface
Type
TCliente = class
private
{ private declarations }
protected
{ protected declarations }
public
{ public declarations }
property IDCliente: Integer;
property Nome: String;
property CNPJ: String;
published
{ published declarations }
end;
implementation
end.
As the aim of the article isn't to show the validation of methods, I'll remove the "garbage" generated by Delphi, then our code will looks like this:
unit Cliente;
interface
Type
TCliente = class
private
FCNPJ: String;
FIDCliente: Integer;
FNome: String;
{ private declarations }
protected
{ protected declarations }
public
{ public declarations }
property IDCliente: Integer read FIDCliente write FIDCliente;
property Nome : String read FNome write FNome;
property CNPJ : String read FCNPJ write FCNPJ;
published
{ published declarations }
end;
implementation
end.
Now, we created TCliente class and their attributes. Let's now, see the creation of our list of Clients, for this we'll create another unit and save it as ListaCliente.pas.
unit ListaCliente; interface implementation end.
With our unit created now, we'll be able to declare our class and his methods. Is very important to remember to declare USES when we use objects / classes that are in other Units
unit ListaCliente;
interface
uses Classes, Cliente, Dialogs;
{Note that here we've done reference the two classes necessary to make the Example }
Type
TListaCliente = class
private
{ private declarations }
FListaClientes : TList;
protected
{ protected declarations }
public
{ public declarations }
constructor Create;
procedure Adicionar(pCliente: TCliente);
procedure Remover(Index: Integer);
function Count: Integer;
published
{ published declarations }
end;
implementation
end.
Now that we declared all, we need implement method to method, right? So let's start with the easiest, our constructor!
constructor TListaCliente.Create; begin inherited Create; FListaClientes := TList.Create; end;
function TListaCliente.Count: Integer; begin Result := FListaClientes.Count; end;
procedure TListaCliente.Adicionar(pCliente: TCliente); begin FListaClientes.Add(pCliente); end;
procedure TListaCliente.Remover(Index: Integer);
begin
if Index < Count then
FListaClientes.Delete(Index)
else
ShowMessage('Item not found!');
end;
With that we already have the list ready to be used, now let's build a small example and put what was seen in the article on practice.
Let's not waste time with screen. Create a form in our application or enjoy of what is already automatically created on Delphi. Change your name to FrmCliente, and save it as uFrmCliente.pas, create a screen with the following interface: View the name of the components are visible.
Figure 1. Layout of Cliente’s form.
First step after the layout are ready, in the FrmCliente we need include the uses in the Cliente and ListaCliente.
unit uFrmCliente; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls, Cliente, ListaCliente;
Now, we need to declare a variable of type TListaCliente order to keep the customers registered on our list.
type
TFrmCliente = class(TForm)
BtnAdicionar: TButton;
BtnRemover: TButton;
BtnContar: TButton;
EdtIDCliente: TLabeledEdit;
EdtNome: TLabeledEdit;
EdtCNPJ: TLabeledEdit;
private
{ Private declarations }
// Variable to store the List
tempListaCliente : TListaCliente;
public
{ Public declarations }
end;
Now, will be necessary to instantiate of our list, for this I'll use the own event from form, the onCreate event, to facilitate implementation.
procedure TFrmCliente.FormCreate(Sender: TObject); begin tempListaCliente := TListaCliente.Create; end;
By now we're able to handle our list, including, removing and counting. Let's start with Add.
procedure TFrmCliente.BtnAdicionarClick(Sender: TObject);
Var
tempCliente : TCliente;
begin
tempCliente := TCliente.Create;
with tempCliente do
begin
IDCliente := StrToInt(EdtIDCliente.Text);
{We used StrToInt to convert the contents of String to Integer}
Nome := EdtNome.Text;
CNPJ := EdtCNPJ.Text;
end;
tempListaCliente.Adicionar(tempCliente);
end;
Now we’ll use the button to remove. Note that I'm using a InputQuery only to select the item to be removed.
procedure TFrmCliente.BtnRemoverClick(Sender: TObject);
Var
tempID: String;
begin
if InputQuery('Remove Cliente','Enter the item to be removed',tempID) then
begin
tempListaCliente.Remover(StrToInt(tempID));
end;
end;
And last but not least is the Count, which will count how many items we have on our list.
procedure TFrmCliente.BtnContarClick(Sender: TObject);
begin
ShowMessage('Total Registration of List: ' + IntToStr(tempListaCliente.Count));
end;
Concluding, in this example, I show the basic way of how we can work with a list of objects in Delphi, running on any version of Delphi.





See the prices for this post in Mr.Bool Credits System below: