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:
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:
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.