Solved

Constructor on TObject descendant

Posted on 2004-04-01
9
523 Views
Last Modified: 2013-11-23
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;
0
Comment
Question by:tomcorcoran
  • 4
  • 3
  • 2
9 Comments
 
LVL 12

Expert Comment

by:esoftbg
ID: 10730735
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
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 10730752
>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
0
 

Author Comment

by:tomcorcoran
ID: 10730821
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;
0
 
LVL 12

Accepted Solution

by:
esoftbg earned 50 total points
ID: 10730875
here are blue dots:

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(AOwner: TWinControl); dynamic;
      destructor  Destroy; override;
    published
  end;

  TThree = class(TTwo)
    private
      Flbl2 : TLabel;
    protected
    public
      constructor Create(AOwner: TWinControl); override;
      destructor Destroy; override;
    published
  end;

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

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

implementation

{$R *.dfm}

constructor TTwo.Create(AOwner: TWinControl);
begin
  inherited Create;
  Flbl := TLabel.Create(AOwner);
  Flbl.Parent := AOwner as TWinControl;
end;

destructor TTwo.Destroy;
begin
  Flbl.Free;
  inherited;
end;

constructor TThree.Create(AOwner: TWinControl);
begin
  inherited Create(AOwner);
  Flbl2 := TLabel.Create(AOwner);
  Flbl2.Parent := AOwner as TWinControl;
end;

destructor TThree.Destroy;
begin
  Flbl2.Free;
  inherited;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Three := TThree.Create(Self);
  Three.Flbl.Font.Style := [fsBold];
  Three.Flbl.Font.Size := 16;
  Three.Flbl.Left := 128;
  Three.Flbl.Top := 96;

  Three.Flbl2.Font.Style := [fsBold];
  Three.Flbl2.Font.Size := 16;
  Three.Flbl2.Left := 128;
  Three.Flbl2.Top := 128;

  Three.Flbl.Caption := 'Hello everybody';
  Three.Flbl2.Caption := 'Ya know it';
end;

end.
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 22

Expert Comment

by:Ferruccio Accalai
ID: 10730890
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....
0
 

Author Comment

by:tomcorcoran
ID: 10730925
How can you override the constructor since it's not virtaul in TObject?
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 10731030
//.........
    public
      constructor Create(AOwner: TWinControl); dynamic;
//.........
0
 
LVL 22

Expert Comment

by:Ferruccio Accalai
ID: 10731049
ah sorry, of course...missed that it's descending from TObject....esoftbg gave the right answer....declaring it dynamic
0
 

Author Comment

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

Cheers, Tom.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now