delphiconstructortform

Delphi Form with custom constructor as the mainform?


I want to have a MainForm that is derived from a BaseForm that has a custom constructor. Since this is the Mainform, it is created by a call to Application.CreateForm(TMyMainForm, MyMainForm) in the *.dpr file. However, my custom constructor is not called during form creation.

Obviously, it works fine, if I call MyMainForm := TMyMainForm.Create(AOwner). Can I not use a form with custom constructor as the main form ?

TBaseForm = class(TForm)
  constructor Create(AOwner:TComponent; AName:string);reintroduce;
end;

TMyMainForm = class(TBaseForm)
  constructor Create(AOwner:TComponent);reintroduce;
end;  

constructor TBaseForm.Create(AOwner:TComponent);

begin;
  inherited Create(AOwner);
end;

constructor TMyMainForm.Create(AOwner:TComponent);

begin;
  inherited Create(AOwner, 'Custom Constructor Parameter');
end;  

Solution

  • Application.CreateForm() cannot call a reintroduce'd constructor. When creating a new object, it calls the TComponent.Create() constructor and expects polymorphism to call any derived constructors. By reintroduce'ing your custom constructor, you are not part of the polymorphic call chain. reintroduce exposes a completely new method that simply has the same name as an inherited method but is not related to it.

    To do what you are attempting, don't use reintroduce for your constructors. Create a separate constructor in your Base form, and then have your MainForm override the standard constructor and call your custom Base constructor, eg:

    TBaseForm = class(TForm)
      constructor CreateWithName(AOwner: TComponent; AName: string); // <-- no reintroduce needed since it is a new name
    end;
    
    TMyMainForm = class(TBaseForm)
      constructor Create(AOwner: TComponent); override; // <-- not reintroduce
    end;  
    
    constructor TBaseForm.CreateWithName(AOwner: TComponent; AName: string);
    begin;
      inherited Create(AOwner);
      // use AName as needed...
    end;
    
    constructor TMyMainForm.Create(AOwner: TComponent);
    begin;
      inherited CreateWithName(AOwner, 'Custom Constructor Parameter');
    end;