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

JustMe picture JustMe · Aug 4, 2011 · Viewed 7.7k times · Source

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?

Answer

ain picture ain · Aug 4, 2011

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