Typecasting issues lazarus / 21-General ---------------------------------------------------------------------- Flea · 2025-04-03 14:23 Two queries! 1) I'm trying to typecast a TEdit control to a TMyEdit so as I can access the protected method KeyPress in its ancestral class (TCustomEdit etc) as in the code below. When it get to the type cast "(Edit1 as TMyEdit).KeyPress" near the end of the code, I get an Invalid Typecast exception. Why is that? Ive tried forcing the typecast with "TMyEdit(Edit1).KeyPress" and this fails with the same exception (compiled under release mode or debug mode). Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode Delphi}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls; type  TForm1 = class(TForm)    Edit1: TEdit;    procedure FormCreate(Sender: TObject);  end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 } type  TMyEdit=class(TEdit)  public    procedure KeyDown(var Key: Word; Shift: TShiftState); override;  end; procedure TMyEdit.KeyDown(var Key: Word; Shift: TShiftState);begin  inherited;end; procedure TForm1.FormCreate(Sender: TObject);var  LKey: word;begin  (Edit1 as TMyEdit).KeyDown(LKey, [ssShift]);end; end.  2) When digging in deeper, I wrote the following test code. When I step into the code near the end "TMySuperClass(LC).DoSomething;" the code execution doesn't step into the TMySuperClass.DoSomething as expected, its steps into the ancestral TMyClass.DoSomething ? Again, does this not work the way Delphi does. I was expecting it to step into TMySuperClass.DoSomething. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode Delphi}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls; type  TForm1 = class(TForm)    procedure FormCreate(Sender: TObject);  end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 }  type  TMyClass=class(Tobject)  protected    procedure DoSomething;virtual;  end;   TMySuperClass=class(TMyClass)  public    procedure DoSomething;override;  end; { MyClass }procedure TMyClass.DoSomething;begin  Inc(gDummy);end; { MySuperClass }procedure TMySuperClass.DoSomething;begin  Inc(gDummy);  inherited;end;  procedure TForm1.FormCreate(Sender: TObject);var  LC: TMyClass;begin  gDummy:=0;  LC:=TMyClass.Create;  TMySuperClass(LC).DoSomething;  LC.Free;end; end. ---------------------------------------------------------------------- jamie · 2025-04-03 14:58 Yes that looks normal to me you never actually created that class but you're using it for a Typecast and if the virtual method so all it's doing is returning back to the very first virtual method that exists which is not your Typecast ---------------------------------------------------------------------- Remy Lebeau · 2025-04-03 15:00 The typecast using the 'as' operator fails because the Edit1 pointer is not pointing at a TMyEdit object, it is pointing at a standard TEdit object. Just because you declared the TMyEdit class does not magically turn Edit1 from a TEdit object into a TMyEdit object. In Delphi, you can use an interposer class (not sure if it works in FreePascal, too?), eg: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode Delphi}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls; type  TEdit=class(StdCtrls.TEdit)  public    procedure KeyDown(var Key: Word; Shift: TShiftState); override;  end;   TForm1 = class(TForm)    Edit1: TEdit;    procedure FormCreate(Sender: TObject);  end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TEdit.KeyDown(var Key: Word; Shift: TShiftState);begin  inherited;end; procedure TForm1.FormCreate(Sender: TObject);var  LKey: word;begin  Edit1.KeyDown(LKey, [ssShift]);end; end. Otherwise, typecasting without the 'as' operator should work just fine (unless FreePascal silently turns the cast into the 'as' operator - Delphi doesn't do that). In which case, you don't need to override the ancestor method in order to call it.  Just use an accessor class to bring the object's protected methods into the calling unit's scope, eg: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode Delphi}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls; type  TForm1 = class(TForm)    Edit1: TEdit;    procedure FormCreate(Sender: TObject);  end; var  Form1: TForm1; implementation {$R *.lfm} type  TEditAccess=class(TEdit)  end; { TForm1 } procedure TForm1.FormCreate(Sender: TObject);var  LKey: word;begin  TEditAccess(Edit1).KeyDown(LKey, [ssShift]);end; end. ---------------------------------------------------------------------- PascalDragon · 2025-04-03 19:10 Quote from: Flea on April 03, 2025, 04:23:07 pm1) I'm trying to typecast a TEdit control to a TMyEdit so as I can access the protected method KeyPress in its ancestral class (TCustomEdit etc) as in the code below. When it get to the type cast "(Edit1 as TMyEdit).KeyPress" near the end of the code, I get an Invalid Typecast exception. Why is that? Ive tried forcing the typecast with "TMyEdit(Edit1).KeyPress" and this fails with the same exception (compiled under release mode or debug mode). Using as is wrong here, because that checks the correct instance of the class. You need to use a normal typecast instead and make sure that $ObjectChecks is disabled (though for a virtual method this wouldn't work anyway, see your second question): Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.FormCreate(Sender: TObject);var  LKey: word;begin  {$Push}{$ObjectChecks Off}  TMyEdit(Edit1).KeyDown(LKey, [ssShift]);  {$Pop}end; Quote from: Flea on April 03, 2025, 04:23:07 pm2) When digging in deeper, I wrote the following test code. When I step into the code near the end "TMySuperClass(LC).DoSomething;" the code execution doesn't step into the TMySuperClass.DoSomething as expected, its steps into the ancestral TMyClass.DoSomething ? Again, does this not work the way Delphi does. I was expecting it to step into TMySuperClass.DoSomething. I would be really surprised if this would work in Delphi, because the point of a virtual method is that it will be called through the VMT which will not change if you typecast the type. ---------------------------------------------------------------------- Remy Lebeau · 2025-04-04 00:55 Quote from: PascalDragon on April 03, 2025, 09:10:12 pmYou need to use a normal typecast instead and make sure that $ObjectChecks is disabled... Oh yeah, I keep forgetting about {$ObjectChecks}! That doesn't exist in Delphi. Quote from: PascalDragon on April 03, 2025, 09:10:12 pmI would be really surprised if this would work in Delphi, because the point of a virtual method is that it will be called through the VMT which will not change if you typecast the type. Agreed.  That code is definitely wrong, trying to force a virtual call on the wrong object type.  In that example, this line: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i LC:=TMyClass.Create; Would need to be changed to this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i LC:=TMySuperClass.Create; Then the rest of the code is fine (but defeats the purpose of using a virtual method). ---------------------------------------------------------------------- Flea · 2025-04-05 11:39 Pascaldragon: Yes, the {$objectchecks off} worked like a charm ! Sorry Remy, your are absolutely correct there. My second example was indeed creating an instance of the wrong class. Doh!  Thanks for your help guys!