<p style="padding:0 0 0 0; margin:0 0 0 0;">Sorry for noise, this was wrong code and even more it has nothing to do with generics.</p>
<p style="padding:0 0 0 0; margin:0 0 0 0;">I can solve it with class helper.</p>
<p style="padding:0 0 0 0; margin:0 0 0 0;"> </p>
<p style="padding:0 0 0 0; margin:0 0 0 0;">V.</p>
<p style="padding:0 0 0 0; margin:0 0 0 0;"> </p>
<p style="padding:0 0 0 0; margin:0 0 0 0;">______________________________________________________________<br />
> Od: "Vojtěch Čihák" <vojtech.cihak@atlas.cz><br />
> Komu: "FPC-Pascal users discussions" <fpc-pascal@lists.freepascal.org><br />
> Datum: 11.12.2018 22:19<br />
> Předmět: [fpc-pascal] Generic (TFPGObjectList) EInvalidCast<br />
></p>
Hi,<br />
<br />
why this code gives EInvalidCast at runtime (but compiles well):w<br />
<br />
program Project1;<br />
{$mode objfpc}{$H+}<br />
<br />
uses<br />
{$IFDEF UNIX}{$IFDEF UseCThreads}<br />
cthreads,<br />
{$ENDIF}{$ENDIF}<br />
Classes, fgl<br />
{ you can add units after this };<br />
<br />
{$R *.res}<br />
type<br />
TMyClass = class<br />
procedure Output1;<br />
end;<br />
<br />
TMyList = class (specialize TFPGObjectList<TMyClass>)<br />
<br />
end;<br />
<br />
TMyDescClass = class(TMyClass)<br />
procedure Output2;<br />
end;<br />
<br />
var MyList: TMyList;<br />
MyClass: TMyClass;<br />
<br />
procedure TMyClass.Output1;<br />
begin<br />
WriteLn('Output1');<br />
end;<br />
<br />
procedure TMyDescClass.Output2;<br />
begin<br />
WriteLn('Output2');<br />
end;<br />
<br />
begin<br />
MyList:=TMyList.Create(True);<br />
MyClass:=TMyClass.Create;<br />
MyList.Add(MyClass);<br />
MyList[0].Output1;<br />
TMyDescClass(MyList[0]); // EInvalidCast<br />
MyList.Free;<br />
end.<br />
<br />
Thanks for help,<br />
<br />
V.<br />
_______________________________________________<br />
fpc-pascal maillist - fpc-pascal@lists.freepascal.org<br />
<a href="http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal">http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal</a>