class - TObjectList<> Get item error -
i'm trying create tobjectlist class in delphi xe8, errors when try value.
compiler error message: "[dcc32 error] : can't access private symbol {system.generics.collections}tlist.getitem"
here 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 :
procedure tform1.button1click(sender: tobject); var l : tlistedetrucs; : 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); := 0 l.count - 1 begin showmessage(l[i].libelle); end; l.delete(0); l.extract(obj); l.free; end;
how can make work ?
well, getitem
, , indeed setitem
private. code cannot see them. private members can seen in unit in declared. need use members @ 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 case class little pointless because none of methods in class vary behaviour base class. peut-ĂȘtre real class more.
Comments
Post a Comment