Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

List of Objects in Delphi

See in this article how we can create a list of objects to store information for multiple objects.

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)

Listing 1: Unit code Cliente to be created


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.

Listing 2: Declaring properties of TCliente class:

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:

Listing 3: Implementing and excluding unnecessary code


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.

Listing 4: unit ListaCliente code to be created


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

Listing 5: Declaring Class and Methods

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!

Listing 6: Implementing Create Method

constructor TListaCliente.Create;
begin
  inherited Create;
  FListaClientes := TList.Create;
end;

Listing 7: Implementing Count Method

function TListaCliente.Count: Integer;
begin
  Result := FListaClientes.Count;
end; 

Listing 8: Implementing Add Method;

procedure TListaCliente.Adicionar(pCliente: TCliente);
begin
  FListaClientes.Add(pCliente);
end; 

Listing 9: Implementing Remove Method

	
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.

List of Objects in Delphi

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.

Listing 10: Uses in the created class.

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.

Listing 11: Creating variable to store the objects.

  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.

Listing 12: Instantiating Client List

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.

Listing 13: onClick event in the BtnAdicionar

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.

Listing 14: onClick event in the BtnRemover.

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.

Listing 15: onClick event in the BtnContar.

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.



Graduated in System Analysis, in business development for over six years, where he worked in companies of various segments such as financial, commercial, parking, etc.. He is currently a third-party developer analyst Navy of Brazi...

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

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

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login