Recent

Author Topic: Initial value of a property in a component  (Read 18357 times)

panoss

  • Full Member
  • ***
  • Posts: 162
  • You only live twice
Initial value of a property in a component
« on: March 10, 2011, 09:46:26 am »
I'm making a control using TCustomControl:
Code: Pascal  [Select][+][-]
  1. constructor myCustomControl.Create(AOwner: TComponent);
  2. begin
  3.    inherited Create(AOwner);
  4.    FAlpha := 255;    
  5. end;
  6.  
The problem is that when the component is created (when I run it), FAlpha does not take the value 255, but the value 0. Why? What should I do?

FAlpha is used in Get an Set of property Alpha:
Code: Pascal  [Select][+][-]
  1. function myCustomControl.GetAlpha: byte;
  2. begin
  3.       Result := FAlpha;
  4. end;
  5. procedure myCustomControl.SetAlpha(newAlpha: byte);
  6. begin      
  7.       FAlpha := newAlpha;          
  8. end;
  9.  
Windows 10 64bit, Lazarus Version 2.2.0    FPC 3.2.2.

eny

  • Hero Member
  • *****
  • Posts: 1659
Re: Initial value of a property in a component
« Reply #1 on: March 10, 2011, 10:58:05 am »
Did you override your custom constructor?

Code: Delphi  [Select][+][-]
  1.   TmyCustomControl = class(... <your parent class>)
  2.     //...
  3.     constructor Create(TheOwner: TComponent); override;
  4.     //...
  5.  
 
All posts based on: Win11; Lazarus 4_4  (x64) 12-02-2026 (unless specified otherwise...)

panoss

  • Full Member
  • ***
  • Posts: 162
  • You only live twice
Re: Initial value of a property in a component
« Reply #2 on: March 10, 2011, 11:17:44 am »
Yes I have overriden it.
« Last Edit: March 10, 2011, 11:19:31 am by panoss »
Windows 10 64bit, Lazarus Version 2.2.0    FPC 3.2.2.

eny

  • Hero Member
  • *****
  • Posts: 1659
Re: Initial value of a property in a component
« Reply #3 on: March 10, 2011, 11:31:07 am »
Did you create a package for the component? I.e. it was added to the component palette?
Or do you create the component at runtime?
If it's the latter, the property will have the correct value immediately after the constructor is called.
All posts based on: Win11; Lazarus 4_4  (x64) 12-02-2026 (unless specified otherwise...)

panoss

  • Full Member
  • ***
  • Posts: 162
  • You only live twice
Re: Initial value of a property in a component
« Reply #4 on: March 10, 2011, 11:44:45 am »
Yes I have created a package for the component and installed it. It's been added at the components palette.
Windows 10 64bit, Lazarus Version 2.2.0    FPC 3.2.2.

eny

  • Hero Member
  • *****
  • Posts: 1659
Re: Initial value of a property in a component
« Reply #5 on: March 10, 2011, 12:18:46 pm »
Did a small test: simple custom component, descendant of TcustomControl and added it to a package.
Added a custom constructor to set a local property and that all worked perfectly!
This is for Window$ though and a fairly recent Lazarus/FPC version (see my sig).
All posts based on: Win11; Lazarus 4_4  (x64) 12-02-2026 (unless specified otherwise...)

panoss

  • Full Member
  • ***
  • Posts: 162
  • You only live twice
Re: Initial value of a property in a component
« Reply #6 on: March 10, 2011, 12:33:14 pm »
Thank you for bothering.
I haven't found why this is happening to me.
May be it's the version of FPC or Lazarus I'm using.
Windows 10 64bit, Lazarus Version 2.2.0    FPC 3.2.2.

Marc

  • Administrator
  • Hero Member
  • *
  • Posts: 2674
Re: Initial value of a property in a component
« Reply #7 on: March 10, 2011, 05:20:42 pm »
this is not fpc/lazarus version dependent.
Somehow your package is not installed. or not used.

How doe you conclude it didn't get the initial value ?
//--
{$I stdsig.inc}
//-I still can't read someones mind
//-Bugs reported here will be forgotten. Use the bug tracker

panoss

  • Full Member
  • ***
  • Posts: 162
  • You only live twice
Re: Initial value of a property in a component
« Reply #8 on: March 10, 2011, 06:38:28 pm »
As I mentioned on the other topic ( :)), I can't make the debugger work properly.
So, in the Setter of the property, and in the MouseDown event,
I've put a line: ShowMessage('A=' + IntToStr(FAlpha));
which shows me the value every time the SetAlpha is called, or MouseDown raised.
« Last Edit: March 10, 2011, 06:45:18 pm by panoss »
Windows 10 64bit, Lazarus Version 2.2.0    FPC 3.2.2.

panoss

  • Full Member
  • ***
  • Posts: 162
  • You only live twice
Re: Initial value of a property in a component
« Reply #9 on: March 11, 2011, 02:42:31 pm »
When I Run the application, firstly is being executed the Constructor.
FAlpha takes the value 255.
BUT, after that is executed the Setter (automatically, I don't call it), where FAlpha := newAlpha;
And newAlpha = 0, so and FAlpha = 0.

I think I will declare newAlpha at the Private section of the component (now it's declared in the Setter, see 1st post), and give it the value I want in the constructor.
Is this the correct way? Any better suggestion?
« Last Edit: March 11, 2011, 02:45:33 pm by panoss »
Windows 10 64bit, Lazarus Version 2.2.0    FPC 3.2.2.

Laksen

  • Hero Member
  • *****
  • Posts: 802
    • J-Software
Re: Initial value of a property in a component
« Reply #10 on: March 11, 2011, 02:48:01 pm »
Remember that components are streamed, so published properties will be set to the value they are given at design time. That would explain why the setter is called after Create

You cannot avoid this behavior for visually/streamed created components, unless you make the property non-published

panoss

  • Full Member
  • ***
  • Posts: 162
  • You only live twice
Re: Initial value of a property in a component
« Reply #11 on: March 11, 2011, 03:23:15 pm »
Laksen, you were correct!! I moved the property from published to public, and everything works as expected (ok , I had to press the 'install' button in the package manager,for the package to be reinstalled, and replace the existing component from my test form with a new one).
Works fine!!Thank you!
Windows 10 64bit, Lazarus Version 2.2.0    FPC 3.2.2.

eny

  • Hero Member
  • *****
  • Posts: 1659
Re: Initial value of a property in a component
« Reply #12 on: March 11, 2011, 04:43:49 pm »
Laksen, you were correct!! I moved the property from published to public, and everything works as expected
Things don't work as expected: they work as designed.
Maybe that was the problem.
All posts based on: Win11; Lazarus 4_4  (x64) 12-02-2026 (unless specified otherwise...)

panoss

  • Full Member
  • ***
  • Posts: 162
  • You only live twice
Re: Initial value of a property in a component
« Reply #13 on: March 11, 2011, 04:55:01 pm »
Ok, you are correct :D.
But how could I know how they are designed?
Windows 10 64bit, Lazarus Version 2.2.0    FPC 3.2.2.

eny

  • Hero Member
  • *****
  • Posts: 1659
Re: Initial value of a property in a component
« Reply #14 on: March 11, 2011, 05:20:28 pm »
Ok, you are correct :D.
But how could I know how they are designed?
The 'problem' lies in the 'expecting'  :D
All posts based on: Win11; Lazarus 4_4  (x64) 12-02-2026 (unless specified otherwise...)

 

TinyPortal © 2005-2018