如何将TDictionary作为可选参数传递?

时间:2013-05-24 17:41:29

标签: delphi delphi-xe2

如何将TDictionary作为可选参数传递?

例如,如果我在一个类中使用它不起作用:

TMyDict = TDictionary<String, String>;

TTest = class
   procedure Render(Id: Integer; Name: String = ''; Variables: TMyDict = nil); //error
end;

我不知道为什么,但这段代码运行正常。但是我无法使用它。

procedure Render(Id: Integer; Name: String = ''; Variables: TMyDict = nil);
begin
   // Do something...
end;

有什么建议吗?

1 个答案:

答案 0 :(得分:5)

您的实施声明缺少课程。它应该是:

procedure TTest.Render(Id: Integer; Name: String = ''; Variables: TMyDict = nil);
//        ^^^^^^
begin
   // Do something...
end;

您也可以考虑省略实现中的默认值。

这是一个完整的程序编译,以说明要点:

program SO16740725;
{$APPTYPE CONSOLE}

uses
  Generics.Collections;

type
  TMyDict = TDictionary<string, string>;

type
  TTest = class
    procedure Render(Id: Integer; Name: string=''; Variables: TMyDict=nil);
  end;

procedure TTest.Render(Id: Integer; Name: string; Variables: TMyDict);
begin
   // Do something...
end;

begin
end.
相关问题