首页 > 解决方案 > delphi属性读取函数加值

问题描述

TApplicationWrapper = class(TObjectWrapper)
private
  function GetMyFonk(): string;
  procedure SetMyFonk(myCmd: string);
published
  property myFonk: String read GetMyFonk write SetMyFonk;

...

function TApplicationWrapper.GetMyFonk(): string;
begin
  ShowMessage('GetMyFonk is Run');
  Result :='';
end;

procedure TApplicationWrapper.SetMyFonk(myCmd: string);
begin
  ShowMessage('SetMyFonk is Run');
end;

该程序以这种方式工作。但我想为GetMyFonk()函数分配参数。

function GetMyFonk (myCommand : String ): string;

我收到一条错误消息。

[dcc32 Error] altPanellerU.pas(74): E2008 Incompatible types

我的屏幕截图

如何为函数赋值?

标签: functionclassdelphiproperties

解决方案


您的属性根本不支持带参数的 getter 函数。对于要添加到 getter 的每个参数,您必须向属性和 setter 添加相应的参数,例如:

TApplicationWrapper = class(TObjectWrapper)
private
  function GetMyFonk(myCommand : String): string;
  procedure SetMyFonk(myCommand : String; Value : string);
published
  property myFonk[myCommand : String] : String read GetMyFonk write SetMyFonk;

...

function TApplicationWrapper.GetMyFonk(myCommand : String): string;
begin
  ShowMessage('GetMyFonk is Run w/ ' + myCommand);
  Result :='';
end;

procedure TApplicationWrapper.SetMyFonk(myCommand : String; Value: string);
begin
  ShowMessage('SetMyFonk is Run w/ ' + myCommand);
end;

然后您必须像这样访问该属性:

App: TApplicationWrapper;
... 
S := App.MyFonk['command'];
... 
App.MyFonk['command'] := S;

这在 Embarcadero 的文档中有更详细的讨论:

属性(德尔福)

请参阅“阵列属性”部分。


推荐阅读