【发布时间】:2020-03-24 00:50:45
【问题描述】:
我有这个:
type
TFuncGetData = function: Integer of object;
function TFrmAPI.GetExactData(AFunc: TFuncGetData): Integer;
var lStatus: Integer;
begin
FErrorMsg := '';
lStatus := AFunc;
...
然后是一堆TFuncGetData这样的
function TFrmAPI.GetCurrentDivision: Integer;
begin
...
被调用:
lResult := GetExactData(GetCurrentDivision);
现在事实证明,所有这些TFuncGetData 都需要一个参数来传递信息:
type
TFuncGetData = function(ASelProps: TSelectionProperties): Integer of object;
function TFrmAPI.GetCurrentDivision(ASelProps: TSelectionProperties): Integer;
begin
...
但函数 GetExactData 现在在 lStatus :=aFunc 调用与 E2035 Not enough actual parameters 时阻塞。
我看到了两种解决方法:
- 定义一个
TFrmAPI.FSelectionProperties,设置它并让GetExactData使用它 - 通过调用传递 TSelectionProperties(如 here):
.
function TFrmAPI.GetExactData(AFunc: TFuncGetData; ASelProps: TSelectionProperties): Integer;
var lStatus: Integer;
begin
FErrorMsg := '';
lStatus := AFunc(ASelProps);
第二种方法更安全(因为可能会覆盖 TFrmAPI.FSelectionProperties),但是还有其他更好的方法来构造它吗?
【问题讨论】:
-
就个人而言,我会使用第二种方法,因为它更具可读性。如果您在执行
GetExactData方法后不需要存储ASelProps,它也很有用。此外,您将能够通过ASelProps传递任何数据,以防将来更改参数数量。 -
我不明白。您想让
GetExactData方法的ASelProps参数可选吗?或者目标是什么(你说“所有这些 TFuncGetData 都需要一个参数” 这是你已经拥有的)?
标签: delphi parameters procedure delphi-10.3-rio