TObjectList <> Get an element error

I am trying to create a TObjectList class in Delphi XE8, but I get errors when I try to get the value.

compiler error message: "[dcc32 Error]: cannot access the 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. 

testing code:

 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?

+6
source share
1 answer

Well, GetItem and indeed SetItem are private. Your code cannot see them. Private members can only be seen in the unit in which they are declared. You must 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 are different from the base class. But peut-Γͺtre your real class does more.

+9
source

Source: https://habr.com/ru/post/985966/


All Articles