gpt4 book ai didi

Delphi - 使用带有属性 setter 的函数

转载 作者:行者123 更新时间:2023-12-03 18:01:34 26 4
gpt4 key购买 nike

Delphi RIO - 定义了一个名为 TBizObj 的类。其中一个属性与 DUNS 编号有关。 DUNS 数字有时会在左侧填充“0”,使其正好为 9 个字符长,因此我有一个名为 SiteDUNS9 的属性(基于 fSiteDUNS9)。调用程序设置 SiteDUNS9 属性,但调用者不必担心 DUNS 是否为 9 个字符,我将在 getter/setter 属性中处理它。

当我定义我的属性来调用此函数时,我收到错误“类型不兼容”。一切都是字符串......不涉及其他类型。这是代码的相关部分:

type
TBizObj = class(TObject)
private
...
fSiteDUNS9: string;
...

function FixDunsLength9(DUNS:string) :string;

published
...
property SiteDUNS9: string read fSiteDUNS9 write FixDunsLength9;

end; // End of the tBizObj Class;

implementation
...
function TBizObj.FixDunsLength9(DUNS:string):string;
begin
// This is a setter function for the DUNS9 routine
result := glib_LeftPad(DUNS, 9, '0');
end;

我已经按照 Embaracadero 网站上的示例进行操作,但仍然无法确定我做错了什么。 http://docwiki.embarcadero.com/RADStudio/Rio/en/Properties_(Delphi)

如果我将属性定义更改为

 property SiteDUNS9:  string read fSiteDUNS9 write fSiteDUNS9;

然后我的程序编译正确。

最佳答案

对于属性 setter ,您需要使用过程 而不是函数。我会按原样保留现有功能,以防您出于其他目的需要它,并为 setter 定义一个单独的过程:

type
TBizObj = class(TObject)
private
...
fSiteDUNS9: string;
...
function FixDunsLength9(const DUNS: string): string;
procedure SetSiteDUNS9(const Value: string);
published
...
property SiteDUNS9: string read fSiteDUNS9 write SetSiteDUNS9;
end;
// End of the tBizObj Class;

implementation

...

function TBizObj.FixDunsLength9(const DUNS: string): string;
begin
Result := glib_LeftPad(DUNS, 9, '0');
end;

procedure TBizObj.SetSiteDUNS9(const Value: string);
var
NewValue: string;
begin
NewValue := FixDunsLength9(Value);
if fSiteDUNS9 <> NewValue then
begin
fSiteDUNS9 := NewValue;
...
end;
end;

关于Delphi - 使用带有属性 setter 的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59445957/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com