boardtc
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;
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;
>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
In my test example it is recognised by compiler and works ?
I see the caption of Flbl on my form....
emil
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;
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:
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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....
override the TTwo and TThree constructor procedures....
ASKER
How can you override the constructor since it's not virtaul in TObject?
//.........
public
constructor Create(AOwner: TWinControl); dynamic;
//.........
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
ASKER
Something is wrong in my setip. The framework takes care of creating the object and somehow the "middle" create is missed.
Cheers, Tom.
Cheers, Tom.
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