Solved

Constructor on TObject descendant

Posted on 2004-04-01
9
526 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delphi cmd execution 6 66
select query - oracle 16 100
SUM 2 INTEGER ARRAYS INTO 1 10 108
Firemonkey webbrowser scrollbars ? 1 49
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

792 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