Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class default property of boolean set to true gives false on run-time

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?

like image 592
JustMe Avatar asked Aug 04 '11 15:08

JustMe


2 Answers

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

like image 179
ain Avatar answered Nov 06 '22 16:11

ain


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.

like image 35
kludg Avatar answered Nov 06 '22 17:11

kludg