function - 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
如何为函数赋值?
解决方案
您的属性根本不支持带参数的 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 的文档中有更详细的讨论:
请参阅“阵列属性”部分。
推荐阅读
- jquery - 根据值选择下拉项不起作用
- python-3.x - 在 Python 脚本中执行 Bash 命令
- php - 在不同页面上显示复选框输出
- python - 在子字符串上加入熊猫数据框
- javascript - 在对象内的数组内追加 - Watson Assistant
- reactjs - 在 React SPA 中将两个子域组合在一起
- javascript - JS 要求用户启用 cookie
- sql-server - 如果不存在则插入触发器
- javascript - 此关键字未指向当前对象
- xamarin.android - ffimageloading.cross.MvxCachedImageView 从 Android 上的资源加载 svg