布尔值的类默认属性设置为 true 在运行时给出 false

发布于 2024-11-27 23:03:23 字数 419 浏览 2 评论 0原文

我有一个带有布尔属性的简单组件类:

  TmyClass = class(TComponent)
    private
      fSomeProperty: boolean;
    published
      property SomeProperty: boolean 
                  read fSomeProperty 
                  write fSomeProperty
                  default true;

  end;

我将其放在表单上,​​将其设置为 true(SomeProperty 设置为 false,为什么?),但是当我试图从运行时访问 SomeProperty 它给了我错误。为什么会这样呢?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沧桑㈠ 2024-12-04 23:03:23

那是因为 default 说明符实际上并不将值分配给属性,它只是告诉流系统哪个值是默认值(因此不需要保存)。您仍然需要将构造函数中的 prop/field 初始化为所需的默认值。这在帮助中有记录,顺便说一句,请阅读“存储说明符”部分

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

离去的眼神 2024-12-04 23:03:23

您还应该在构造函数中将该属性设置为 True - 否则会出现错误:

constructor TMyClass.Create(AOwner: TComponent);
begin
  inherited;
  FSomeProperty:= True;
  ...
end;

默认值决定 *.DFM 文件中将存储的内容。如果您在设计时将 FSomeProperty 设置为 True,并且 FSomeProperty 的默认值为 True,则 FSomeProperty 将不会存储在 *.DFM 中。

如果您没有在构造函数中将 FSomeProperty 初始化为 True,则会出现您所描述的错误 - FSomeProperty 出现 False在运行时,尽管它在设计时设置为True

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文