I've got a simple component class with boolean property:
TmyClass = class(TComponent)
private
fSomeProperty: boolean;
published
property SomeProperty: boolean
read fSomeProperty
write fSomeProperty
default true;
end;
I put it on my form, set it to true (SomeProperty is set to false, why?), but when i'm trying to access SomeProperty from run-time it's giving me false. Why is that so?
Thats because the default
specifier don't actually assign the value to the property, it just says to the streaming system which value is the default (and thus doesn't need to be saved). You still have to initialize the prop/field in the constructor to the desired default value. This is documented in the help btw, read the "Storage Specifiers" section
You should also set the property to True in the constructor - otherwise it is an error:
constructor TMyClass.Create(AOwner: TComponent);
begin
inherited;
FSomeProperty:= True;
...
end;
Default values determine what will be stored in *.DFM file. If you set FSomeProperty
to True
at design time, and default value for FSomeProperty
is True
, then FSomeProperty
will not be stored in *.DFM.
If you don't initialize FSomeProperty
to True
in the constructor you get an error you described - FSomeProperty
appears False
at runtime, though it was set True
at design time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With