Reputation: 73
We are working on creating custom component in firemonkey on Delphi Xe2 platform. I am creating published property with firemonkey custom component. I have set default value such as Boolean, custom type, but I am setting default integer value. I am using class TControl1 = class(TControl). Published Property Test : Integer read FTest write Set Test default 10; On component viewer my custom component shows 0 default.Sorry for my bad english. Please anyone help me
Upvotes: 2
Views: 588
Reputation: 613392
Specifying the default
property value does not assign that value to the property at runtime. All it does is control how the property is stored. If the property's value is equal to the default when the property is stored, then the VCL streaming framework omits the property.
The documentation says it like this:
When you declare a property, you can specify a default value for it. The VCL uses the default value to determine whether to store the property in a form file. If you do not specify a default value for a property, the VCL always stores the property.
...
Declaring a default value does not set the property to that value. The component's constructor method should initialize property values when appropriate. However, since objects always initialize their fields to 0, it is not strictly necessary for the constructor to set integer properties to 0, string properties to null, or Boolean properties to False.
In other words, you must initialise the property in the component's constructor, in addition to setting the default value. And the onus is on you to ensure that you initialise it to the same value as you specified as the default
.
I have personally always found the duplication inherent in the design to be somewhat frustrating. The designers have succeeded in building into the language a violation of the DRY principle. The very fact that you have asked this question illustrates the weakness of the design. Having specified the default
value you are surprised that the compiler appears to ignore you and demands that you set it again.
Upvotes: 3
Reputation: 4776
If I remember correctly, default
directive does not set your private member, FTest
. You have to initialize it in your component's constructor like this:
TControl1 = class(TControl)
private
FTest: Integer;
procedure SetTest(Value: Integer);
public
constructor Create(AOwner: TComponent); override;
published
property Test: Integer read FTest write SetTest default 10;
end;
.
.
.
constructor TControl1.Create;
begin
inherited;
FTest := 10;
end;
Upvotes: 4