Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TObjectList<> Get item error

I'm trying to create TObjectList class in Delphi XE8, but i get errors when i try to get the value.

compiler error message: "[dcc32 Error] : can't access to private symbol {System.Generics.Collections}TList.GetItem"

Here is my code:

unit Unit2;
interface
uses
  Classes, System.SysUtils, System.Types, REST.Types, System.JSON, Data.Bind.Components,
  System.RegularExpressions, System.Variants,
  Generics.Collections;

  type
  TTruc = class
  public
    libelle : string;
    constructor Create(pLibelle : string);
  end;

  TListeDeTrucs = class(TObjectList<TTruc>)
  private
    function GetItem(Index: Integer): TTruc;
    procedure SetItem(Index: Integer; const Value: TTruc);
  public
    function Add(AObject: TTruc): Integer;
    procedure Insert(Index: Integer; AObject: TTruc);
    procedure Delete(Index: Integer);
    property Items[Index: Integer]: TTruc read GetItem write SetItem; default;
  end;

implementation

{ TTruc }

constructor TTruc.Create(pLibelle: string);
begin
  libelle := pLibelle;
end;

{ TListeDeTrucs }

function TListeDeTrucs.Add(AObject: TTruc): Integer;
begin
  result := inherited Add(AObject);
end;

procedure TListeDeTrucs.Insert(Index: Integer; AObject: TTruc);
begin
  inherited Insert(index, AObject);
end;

procedure TListeDeTrucs.Delete(Index: Integer);
begin
  inherited delete(index);
end;

function TListeDeTrucs.GetItem(Index: Integer): TTruc;
begin
  result := inherited GetItem(index);
end;

procedure TListeDeTrucs.SetItem(Index: Integer; const Value: TTruc);
begin
  inherited setItem(index, value);
end;
end.

the testing code is :

procedure TForm1.Button1Click(Sender: TObject);
var
  l : TListeDeTrucs;
  i : integer;
  Obj : TTruc;
begin
  l := TListeDeTrucs.Create(true);
  l.Add(TTruc.Create('one'));
  l.Add(TTruc.Create('two'));
  Obj := TTruc.Create('three');
  l.Add(Obj);
  for i := 0 to l.count - 1 do
  begin
    showMessage(l[i].libelle);
  end;
  L.Delete(0);
  l.extract(Obj);
  l.Free;
end;

How can i make it work ?

like image 660
Fabien Fert Avatar asked Apr 25 '15 20:04

Fabien Fert


1 Answers

Well, GetItem, and indeed SetItem are private. Your code cannot see them. Private members can be seen only in the unit in which they are declared. You need to use members that are at least protected.

This compiles:

function TListeDeTrucs.GetItem(Index: Integer): TTruc;
begin
  Result := inherited Items[Index];
end;

procedure TListeDeTrucs.SetItem(Index: Integer; const Value: TTruc);
begin
  inherited Items[Index] := Value;
end;

In this case your class is a little pointless because none of the methods in your class vary behaviour from the base class. But peut-être your real class does more.

like image 175
David Heffernan Avatar answered Nov 05 '22 07:11

David Heffernan