Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SerialForms.pas(17): W1010 Method 'Create' hides virtual method of base type 'TComponent'

I created a class

  FormInfo = class (TComponent)
  private
    FLeftValue : Integer;
    FTopValue : Integer;
    FHeightValue : Integer;
    FWidthValue : Integer;
  public
    constructor Create(
      AOwner : TComponent;
      leftvalue : integer;
      topvalue : integer;
      heightvalue : integer;
      widthvalue : integer);
  protected
    procedure GetChildren(Proc: TGetChildProc; Root: TComponent); override;
    function  GetChildOwner: TComponent; override;
    //procedure SetParentComponent(Value : TComponent); override;
  published
    property LeftValue : Integer read FLeftValue write FLeftValue;
    property TopValue : Integer read FTopValue write FTopValue;
    property HeightValue : Integer read FHeightValue write FHeightValue;
    property WidthValue : Integer read FWidthValue write FWidthValue;
  end;

which is used further for form serialization. The Create method has the following implementation

constructor FormInfo.Create(AOwner: TComponent; leftvalue, topvalue, heightvalue,
  widthvalue: integer);
begin
  inherited Create(AOwner);

  FLeftValue := leftvalue;
  FTopValue := topvalue;
  FHeightValue := heightvalue;
  FWidthValue := widthvalue;
end;

As a result of assembly I receive warning

[dcc32 Warning] SerialForms.pas(17): W1010 Method 'Create' hides virtual method of base type 'TComponent'

What it is necessary to make to get rid of this warning without loss of functionality of application?

like image 999
user1851132 Avatar asked Dec 27 '22 12:12

user1851132


1 Answers

Use the reintroduce reserved word to indicate the compiler you want to intentionally hide the base class constructor in your class:

TMyClass = class (TComponent)
public
  constructor Create(AOwner: TComponent; MyParam: Integer; Other: Boolean); reintroduce;

That way, no warning is shown.

That said, you have to re-think hiding the TComponent.Create constructor. It is a bad idea since the default TComponent.Constructor is called by Delphi to create your component instances at run-time when added to forms/data modules at design time.

TComponent makes the constructor virtual to allow you execute custom code during that process, but you have to stick with the Create firm passing you only the owner, and let the streaming mechanism to deal with the stored values for properties after the creation is complete.

Your component have to support being "unconfigured" if that's the case, or setup default values for it's properties in this universal constructor.

You can provide more constructors, with different names, to allow you create instances at run-time from code passing values for the different properties for your convenience.

like image 178
jachguate Avatar answered Dec 31 '22 11:12

jachguate