Delphi-使用带有属性设置器的函数

问题描述 投票:0回答:1

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;

然后我的程序正确编译。

delphi properties setter
1个回答
0
投票

您需要声明setter方法的过程。正如Property Access帮助所说:

write fieldOrMethod

write说明符中,如果fieldOrMethod是方法,则它必须是采用单个值或相同的const参数的过程作为属性输入(或更多,如果它是数组属性或已索引)财产)。

根据您的情况,您可以像这样写一个setter:

Property Access

但是遵循命名约定,我建议您将设置器命名为type TBizObj = class(TObject) private FSiteDUNS9: string; procedure FixDunsLength9(const DUNS: string): string; published property SiteDUNS9: string read fSiteDUNS9 write FixDunsLength9; end; implementation function TBizObj.FixDunsLength9(const DUNS: string): string; begin if DUNS <> FSiteDUNS9 then begin DoWhatYouNeed; FSiteDUNS9 := DUNS; end; end; ,并使用参数调用SetSiteDUNS9

© www.soinside.com 2019 - 2024. All rights reserved.