否定属性默认好像不行
Negative property default does not seem to work
当我安装组件时,我在对象检查器中查看 StoppingCount
的值为 0!我需要的值为-1。在我的代码中,高于 -1 的任何值都会在该数字处停止 for 循环过程。
default
对负数不起作用吗?
unit myUnit;
interface
uses
System.SysUtils, System.Classes;
type
TmyComponent = class(TComponent)
private
{ Private declarations }
FStoppingCount: integer;
protected
{ Protected declarations }
procedure ProcessIT();
public
{ Public declarations }
published
{ Published declarations }
property StoppingCount: integer read FStoppingCount write FStoppingCount default -1;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('myComponent', [TmyComponent]);
end;
procedure TmyComponent.ProcessIT();
begin
for I := 0 to 1000 do
begin
DoSomething();
if FStoppingCount = I then break;
end;
end;
负值工作得很好。问题是您实际上并未将 FStoppingCount
初始化为 -1,因此当创建组件的新实例并将其内存最初清零时,它会被初始化为 0。
仅仅在 property
声明中声明一个非零 default
值是不够的。 default
值仅存储在 属性 的 RTTI 中,仅在将组件写入 DFM 时以及在对象中显示 属性 值时用于比较目的检查员。 default
指令实际上并不影响内存中组件的实例。您必须明确设置 FStoppingCount
的值以匹配 default
值。这在文档中有明确说明:
Note: Property values are not automatically initialized to the default value. That is, the default directive controls only when property values are saved to the form file, but not the initial value of the property on a newly created instance.
要修复你的组件,你需要添加一个初始化FStoppingCount
为-1的构造函数,例如:
unit myUnit;
interface
uses
System.SysUtils, System.Classes;
type
TmyComponent = class(TComponent)
private
{ Private declarations }
FStoppingCount: integer;
protected
{ Protected declarations }
procedure ProcessIT();
public
{ Public declarations }
constructor Create(AOwner: TComponent); override; // <-- ADD THIS!
published
{ Published declarations }
property StoppingCount: integer read FStoppingCount write FStoppingCount default -1;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('myComponent', [TmyComponent]);
end;
constructor TmyComponent.Create(AOwner: TComponent);
begin
inherited;
FStoppingCount := -1; // <-- ADD THIS!
end;
procedure TmyComponent.ProcessIT();
begin
for I := 0 to 1000 do
begin
DoSomething();
if FStoppingCount = I then break;
end;
end;
当我安装组件时,我在对象检查器中查看 StoppingCount
的值为 0!我需要的值为-1。在我的代码中,高于 -1 的任何值都会在该数字处停止 for 循环过程。
default
对负数不起作用吗?
unit myUnit;
interface
uses
System.SysUtils, System.Classes;
type
TmyComponent = class(TComponent)
private
{ Private declarations }
FStoppingCount: integer;
protected
{ Protected declarations }
procedure ProcessIT();
public
{ Public declarations }
published
{ Published declarations }
property StoppingCount: integer read FStoppingCount write FStoppingCount default -1;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('myComponent', [TmyComponent]);
end;
procedure TmyComponent.ProcessIT();
begin
for I := 0 to 1000 do
begin
DoSomething();
if FStoppingCount = I then break;
end;
end;
负值工作得很好。问题是您实际上并未将 FStoppingCount
初始化为 -1,因此当创建组件的新实例并将其内存最初清零时,它会被初始化为 0。
仅仅在 property
声明中声明一个非零 default
值是不够的。 default
值仅存储在 属性 的 RTTI 中,仅在将组件写入 DFM 时以及在对象中显示 属性 值时用于比较目的检查员。 default
指令实际上并不影响内存中组件的实例。您必须明确设置 FStoppingCount
的值以匹配 default
值。这在文档中有明确说明:
Note: Property values are not automatically initialized to the default value. That is, the default directive controls only when property values are saved to the form file, but not the initial value of the property on a newly created instance.
要修复你的组件,你需要添加一个初始化FStoppingCount
为-1的构造函数,例如:
unit myUnit;
interface
uses
System.SysUtils, System.Classes;
type
TmyComponent = class(TComponent)
private
{ Private declarations }
FStoppingCount: integer;
protected
{ Protected declarations }
procedure ProcessIT();
public
{ Public declarations }
constructor Create(AOwner: TComponent); override; // <-- ADD THIS!
published
{ Published declarations }
property StoppingCount: integer read FStoppingCount write FStoppingCount default -1;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('myComponent', [TmyComponent]);
end;
constructor TmyComponent.Create(AOwner: TComponent);
begin
inherited;
FStoppingCount := -1; // <-- ADD THIS!
end;
procedure TmyComponent.ProcessIT();
begin
for I := 0 to 1000 do
begin
DoSomething();
if FStoppingCount = I then break;
end;
end;