Jus*_*tMe 8 delphi delphi-2010
我有一个带布尔属性的简单组件类:
TmyClass = class(TComponent)
private
fSomeProperty: boolean;
published
property SomeProperty: boolean
read fSomeProperty
write fSomeProperty
default true;
end;
Run Code Online (Sandbox Code Playgroud)
我把它放在我的表单上,将其设置为true(SomeProperty设置为false,为什么?),但是当我尝试从运行时访问SomeProperty时它会给我错误.为什么会这样?
您还应该在构造函数中将该属性设置为True - 否则它是一个错误:
constructor TMyClass.Create(AOwner: TComponent);
begin
inherited;
FSomeProperty:= True;
...
end;
Run Code Online (Sandbox Code Playgroud)
默认值确定将存储在*.DFM文件中的内容.如果设置FSomeProperty为True设计时,默认值为FSomePropertyis True,则FSomeProperty不会存储在*.DFM中.
如果未在构造函数中初始化FSomeProperty,则会出现True您所描述的错误 - 在运行时FSomeProperty出现False,尽管它是True在设计时设置的.