Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2018-12

Generic (TFPGObjectList) EInvalidCast

5 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Vojtěch Čihák
Hi,
 
why this code gives EInvalidCast at runtime (but compiles well):w
 
program Project1;
{$mode objfpc}{$H+}
 
uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  Classes, fgl
  { you can add units after this };
 
{$R *.res}
type
  TMyClass = class
    procedure Output1;
  end;
 
  TMyList = class (specialize TFPGObjectList<TMyClass>)
 
  end;
 
  TMyDescClass = class(TMyClass)
    procedure Output2;
  end;
 
var MyList: TMyList;
    MyClass: TMyClass;
 
procedure TMyClass.Output1;
begin
  WriteLn('Output1');
end;
 
procedure TMyDescClass.Output2;
begin
  WriteLn('Output2');
end;
 
begin
  MyList:=TMyList.Create(True);
  MyClass:=TMyClass.Create;
  MyList.Add(MyClass);
  MyList[0].Output1;
  TMyDescClass(MyList[0]);  // EInvalidCast
  MyList.Free;
end.
 
Thanks for help,
 
V.
Vojtěch Čihák

I forgot: FreePascal 3.3.1 r.40507
 
V.
______________________________________________________________

Show quoted text 5 lines
Od: "Vojtěch Čihák" Komu: "FPC-Pascal users discussions" Datum: 11.12.2018 22:19 Předmět: [fpc-pascal] Generic (TFPGObjectList) EInvalidCast
Hi,
  
 why this code gives EInvalidCast at runtime (but compiles well):w
  
 program Project1;
 {$mode objfpc}{$H+}
  
 uses
   {$IFDEF UNIX}{$IFDEF UseCThreads}
   cthreads,
   {$ENDIF}{$ENDIF}
   Classes, fgl
   { you can add units after this };
  
 {$R *.res}
 type
   TMyClass = class
     procedure Output1;
   end;
  
   TMyList = class (specialize TFPGObjectList<TMyClass>)
  
   end;
  
   TMyDescClass = class(TMyClass)
     procedure Output2;
   end;
  
 var MyList: TMyList;
     MyClass: TMyClass;
  
 procedure TMyClass.Output1;
 begin
   WriteLn('Output1');
 end;
  
 procedure TMyDescClass.Output2;
 begin
   WriteLn('Output2');
 end;
  
 begin
   MyList:=TMyList.Create(True);
   MyClass:=TMyClass.Create;
   MyList.Add(MyClass);
   MyList[0].Output1;
   TMyDescClass(MyList[0]);  // EInvalidCast
   MyList.Free;
 end.
  
 Thanks for help,
  
 V.
Vojtěch Čihák

Sorry for noise, this was wrong code and even more it has nothing to do with generics. I can solve it with class helper.
 
V.
 
______________________________________________________________

Show quoted text 5 lines
Od: "Vojtěch Čihák" Komu: "FPC-Pascal users discussions" Datum: 11.12.2018 22:19 Předmět: [fpc-pascal] Generic (TFPGObjectList) EInvalidCast
Hi,
  
 why this code gives EInvalidCast at runtime (but compiles well):w
  
 program Project1;
 {$mode objfpc}{$H+}
  
 uses
   {$IFDEF UNIX}{$IFDEF UseCThreads}
   cthreads,
   {$ENDIF}{$ENDIF}
   Classes, fgl
   { you can add units after this };
  
 {$R *.res}
 type
   TMyClass = class
     procedure Output1;
   end;
  
   TMyList = class (specialize TFPGObjectList<TMyClass>)
  
   end;
  
   TMyDescClass = class(TMyClass)
     procedure Output2;
   end;
  
 var MyList: TMyList;
     MyClass: TMyClass;
  
 procedure TMyClass.Output1;
 begin
   WriteLn('Output1');
 end;
  
 procedure TMyDescClass.Output2;
 begin
   WriteLn('Output2');
 end;
  
 begin
   MyList:=TMyList.Create(True);
   MyClass:=TMyClass.Create;
   MyList.Add(MyClass);
   MyList[0].Output1;
   TMyDescClass(MyList[0]);  // EInvalidCast
   MyList.Free;
 end.
  
 Thanks for help,
  
 V.
Mattias Gaertner
to Vojtěch Čihák:   why this code gives EInvalidCast at runtime (but compiles well):w   program Project1;
Show quoted text 47 lines
Hi,   why this code gives EInvalidCast at runtime (but compiles well):w   program Project1; {$mode objfpc}{$H+}   uses   {$IFDEF UNIX}{$IFDEF UseCThreads}   cthreads,   {$ENDIF}{$ENDIF}   Classes, fgl   { you can add units after this };   {$R *.res} type   TMyClass = class     procedure Output1;   end;     TMyList = class (specialize TFPGObjectList<TMyClass>)     end;     TMyDescClass = class(TMyClass)     procedure Output2;   end;   var MyList: TMyList;     MyClass: TMyClass;   procedure TMyClass.Output1; begin   WriteLn('Output1'); end;   procedure TMyDescClass.Output2; begin   WriteLn('Output2'); end;   begin   MyList:=TMyList.Create(True);   MyClass:=TMyClass.Create;   MyList.Add(MyClass);   MyList[0].Output1;   TMyDescClass(MyList[0]);  // EInvalidCast

because MyClass is not TMyDescClass

Mattias

Vojtěch Čihák
to Vojtěch Čihák:   why this code gives EInvalidCast at runtime (but compiles well):w   program Project1;

Yes, I see now, it was my mistake.
 
Thanks.
______________________________________________________________

Show quoted text 5 lines
Od: "Mattias Gaertner via fpc-pascal" Komu: Datum: 11.12.2018 22:35 Předmět: Re: [fpc-pascal] Generic (TFPGObjectList) EInvalidCast

On Tue, 11 Dec 2018 22:19:11 +0100
Vojtěch Čihák wrote:

because MyClass is not TMyDescClass

Mattias