Delphi程序参数的默认值
我有一个这样的程序。内容如下:Delphi程序参数的默认值,delphi,Delphi,我有一个这样的程序。内容如下: procedure CaseListMyShares(search: String); 我想用两种方法调用这个函数,一种是不带任何参数,另一种是带参数。我为过程找到了重载关键字,但我不想两次编写同一个函数 如果我调用这个过程,比如CaseListMyShares(“”),它可以工作 但我可以在德尔福做下面这件事吗 procedure TFormMain.CaseListMyShares(search: String); var i: Integer; beg
procedure CaseListMyShares(search: String);
我想用两种方法调用这个函数,一种是不带任何参数,另一种是带参数。我为过程找到了重载
关键字,但我不想两次编写同一个函数
如果我调用这个过程,比如CaseListMyShares(“”)代码>,它可以工作
但我可以在德尔福做下面这件事吗
procedure TFormMain.CaseListMyShares(search: String);
var
i: Integer;
begin
myShares := obAkasShareApiAdapter.UserShares('1', search, '', '');
MySharesGrid.RowCount:= Length(myShares) +1;
MySharesGrid.AddCheckBoxColumn(0, false);
MySharesGrid.AutoFitColumns;
for i := 0 to Length(myShares) -1 do
begin
MySharesGrid.Cells[1, i+1]:= myShares[i].patientCase;
MySharesGrid.Cells[2, i+1]:= myShares[i].sharedUser;
MySharesGrid.Cells[3, i+1]:= myShares[i].creationDate;
MySharesGrid.Cells[4, i+1]:= statusText[StrToInt(myShares[i].situation) -1];
MySharesGrid.Cells[5, i+1]:= '';
MySharesGrid.Cells[6, i+1]:= '';
end;
end;
并致电:
procedure TFormMain.CaseListMyShares(search = '': String);
有两种方法可以实现这一点。这两种方法都很有用,而且通常可以互换。然而,在某些情况下,其中一种或另一种更可取,因此下面两种技术都值得了解
默认参数值
其语法如下所示:
CaseListMyShares();
您可以这样调用该方法:
procedure DoSomething(Param: string = '');
以上两种行为方式相同。实际上,当编译器遇到DoSomething()
时,它只是替换默认参数值并编译代码,就像您编写了DoSomething(“”)
一样
文档:
重载方法
这些方法的实施方式如下:
procedure DoSomething(Param: string); overload;
procedure DoSomething; overload;
注意,逻辑的主体仍然只实现一次。以这种方式编写重载时,将有一个主方法执行该工作,还有许多其他重载调用该主方法`
文档:.使用过程TFormMain.CaseListMyShares(const Search:string='')代码>@Victoria谢谢你,为什么我们要使用const?我建议你阅读和主题。@Victoria一如既往地给出了非常好的评论。但是你为什么不再回答了?。我发现仅仅停留在评论上是浪费你的才能/经验。很明显,你仍然想帮忙,为什么不回答呢。只是好奇:)@Nasreddine,谢谢你!但我的答案就是我在评论中发表的那两句话。不过我还是在关注FireDAC的标签。许多问题需要更深入的分析,而不仅仅是重写记录的内容(我更喜欢这样,因为当我们发现问题时,它可以使FireDAC比以前更好):)
procedure DoSomething(Param: string); overload;
procedure DoSomething; overload;
procedure TMyClass.DoSomething(Param: string);
begin
// implement logic of the method here
end;
procedure TMyClass.DoSomething;
begin
DoSomething('');
end;