Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 4 of 4 total
Thread Question about component initialization
Wed, Aug 9 2017 3:55 AMPermanent Link

Michael Dreher

Hi all.
I've a component placed on a (main) form with a published "port" property (set to value 9080 in the object inspector). In the constructor of the component a default value is set to 80.

type
 TftUpdateCheck = class(TComponent)
 private
   FServerPort : integer;
   // ...
 public published
   property  ServerPort : integer read  FServerPort write FServerPort;
 end;

constructor TftUpdateCheck.Create(aOwner : TComponent);
begin
 Inherited Create(aOwner);
 FServerPort := 80; // default port
end;

I now want the component do a server request itself using the customized port value 9080 as soon as the component is initialized and the value can read out from FServerPort, eg DoRequest(FServerPort).
Do a call in the component's constructor? No, the port value still is 80 here. I tried oberloading...

procedure TftUpdateCheck.AfterInitialize;
begin
 inherited AfterInitialize;
 InformationDialog('AI: ' + IntToStr(FServerPort));  // getting the value 0 here
end;

I tried oberloading...

function TftUpdateCheck.LoadProperty(AReader: TReader): Boolean;
begin
 Result := inherited LoadProperty(AReader);
 InformationDialog('LP: ' + IntToStr(FServerPort));
end;

but it's called multible times with port value 80 first and later with the value 9080.

How to implement this? Thanks for replies.
M. Dreher
Wed, Aug 9 2017 7:27 AMPermanent Link

Uli Becker

Michael,

> constructor TftUpdateCheck.Create(aOwner : TComponent);
> begin
>    Inherited Create(aOwner);
>    FServerPort := 80; // default port
> end;

Not at my computer, but:

normally properties are initialized within the "InitializeProperties"
procedure:

procedure TftUpdateCheck.InitializeProperties;
begin
   inherited InitializeProperties;
   FServerPort := 80; // default port
end;

Maybe that makes a difference and you can access the 9080-value in the
OnCreate event of the component.
In addition: there is an "AfterLoad" event for controls. I've never used
it but it might be helpful.

Uli
Wed, Aug 9 2017 8:44 AMPermanent Link

Michael Dreher

Uli Becker wrote:

 // normally properties are initialized within the "InitializeProperties"
 //
 // procedure TftUpdateCheck.InitializeProperties;
 // begin
 //     inherited InitializeProperties;
 //    FServerPort := 80; // default port
 // end;

I checked this and got the value FServerPort = 0 in InitializeProperties. Note: members of  cass instances always initialized in the constructur with default values (null, 0, emty string).

 //  Maybe that makes a difference and you can access the 9080-value in the OnCreate event of the component.

OnCreate is for TFormControl descendants; my component is "only" a TComponent .

 // In addition: there is an "AfterLoad" event for controls. I've never used it but it might be helpful.

Year! AfterLoad ist called exactly once and I get the right property value from the object inspector. Thanks.
Michael Dreher
Wed, Aug 9 2017 9:58 AMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Michael,

<< Year! AfterLoad ist called exactly once and I get the right property value from the object inspector. >>

Yes, the AfterLoad method is what you want.  I need to put together a component writers guide for things like this because it can get confusing, even for me.  There are different layers that are used independently in some cases, and together in others, depending upon whether a component is being created (BeforeInitialize/AfterInitialize) or loaded (BeforeLoad/AfterLoad).

Tim Young
Elevate Software
www.elevatesoft.com
Image