Link to home
Start Free TrialLog in
Avatar of boardtc
boardtcFlag for Ireland

asked on

Constructor on TObject descendant

The cReate for a TObject descendant is not getting fired, in fact it's not trecognised by the complier (no blue dots). here's some sample code. What have I done wrong?

Thanks, Tom.

  TOne = class(TObject)
  private
  protected
  public
  published
  end;


  TTwo = class(TOne)
  private
    Flbl : TLabel;
  protected
  public
    constructor create;
    destructor destroy; override;
  published
  end;

constructor TTwo .create;
begin
  inherited create;

  Flbl := TLabel.Create(nil);
end;

destructor TTwo .destroy;
begin
  Flbl.free;
  inherited;
end;
Avatar of esoftbg
esoftbg
Flag of Bulgaria image

unit Unit_Q_20939766;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TOne = class(TObject)
    private
    protected
    public
    published
  end;

  TTwo = class(TOne)
    private
      Flbl : TLabel;
    protected
    public
      constructor create;
      destructor destroy; override;
    published
  end;

  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
    private { Private declarations }
    public  { Public declarations }
  end;

var
  Form1: TForm1;
  Two:   TTwo;

implementation

{$R *.dfm}

constructor TTwo .create;
begin
  inherited create;

  Flbl := TLabel.Create(nil);
end;

destructor TTwo .destroy;
begin
  Flbl.free;
  inherited;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Two := TTwo.Create;
  Two.Flbl.Parent := Self;
  Two.Flbl.Caption := 'Hello everybody';
end;

end.

emil
>in fact it's not trecognised by the complier (no blue dots).
In my test example it is recognised by compiler and works ?
I see the caption of Flbl on my form....

emil
Avatar of boardtc

ASKER

Ok, not sure what I've missed here :-) Must be something else in my code I am missing.

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TOne = class(TObject)
    private
    protected
    public
    published
  end;

  TTwo = class(TOne)
    private
      Flbl : TLabel;
    protected
    public
      constructor create;
      destructor destroy; override;
    published
  end;


  TThree = class(TTwo)
    private
      Flbl2 : TLabel;
    protected
    public
      constructor create;
      destructor destroy; override;
    published
  end;

  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    private { Private declarations }
    public  { Public declarations }
  end;

var
  Form1: TForm1;
  Two:   TTwo;
  Three:   TThree;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  Two := TTwo.Create;
  Two.Flbl.Parent := Self;
  Two.Flbl.Caption := 'Hello everybody';

  Three := Three.Create;
  Three.Flbl.Parent := Self;
  Three.Flbl.Caption := 'Ya know it';
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  Three.Free;
  Two.Free;
end;

{ TTwo }

constructor TTwo.create;
begin
  inherited create;

  Flbl := TLabel.Create(nil);
end;

destructor TTwo.destroy;
begin
  Flbl.free;
  inherited;
end;

{ TThree }

constructor TThree.create;
begin
  inherited create;

  Flbl2 := TLabel.Create(nil);
end;

destructor TThree.destroy;
begin
  Flbl2.free;
  inherited;
end;



end.

 I get no blue dots on the last destructor :

destructor TThree.destroy;
begin
  Flbl2.free;
  inherited;
end;
ASKER CERTIFIED SOLUTION
Avatar of esoftbg
esoftbg
Flag of Bulgaria image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Ferruccio Accalai
you're calling create inheriting it but how can you inherit a non overrided procedure? It's never inherited so....

override the TTwo and TThree constructor procedures....
Avatar of boardtc

ASKER

How can you override the constructor since it's not virtaul in TObject?
//.........
    public
      constructor Create(AOwner: TWinControl); dynamic;
//.........
ah sorry, of course...missed that it's descending from TObject....esoftbg gave the right answer....declaring it dynamic
Avatar of boardtc

ASKER

Something is wrong in my setip. The framework takes care of creating the object and somehow the "middle" create is missed.

Cheers, Tom.