Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
delphi中的匿名记录构造函数_Delphi_Anonymous Types - Fatal编程技术网

delphi中的匿名记录构造函数

delphi中的匿名记录构造函数,delphi,anonymous-types,Delphi,Anonymous Types,是否可以将记录用作方法参数,并在不隐式声明所述记录的实例的情况下调用它 我希望能够编写这样的代码 type TRRec = record ident : string; classtype : TClass; end; procedure Foo(AClasses : array of TRRec); 然后像这样或类似的方法调用 Foo([('Button1', TButton), ('Lable1', TLabel)]); 顺便说一句,我仍然停留在Delphi 5

是否可以将记录用作方法参数,并在不隐式声明所述记录的实例的情况下调用它

我希望能够编写这样的代码

type
  TRRec = record
    ident : string;
    classtype : TClass;
  end;

procedure Foo(AClasses : array of TRRec);
然后像这样或类似的方法调用

Foo([('Button1', TButton), ('Lable1', TLabel)]);
顺便说一句,我仍然停留在Delphi 5上。

是的。差不多

type
  TRRec = record
    ident : string;
    classtype : TClass;
  end;

function r(i: string; c: TClass): TRRec;
begin
  result.ident     := i;
  result.classtype := c;
end;

procedure Foo(AClasses : array of TRRec);
begin
  ;
end;

// ...
Foo([r('Button1', TButton), r('Lable1', TLabel)]);

也可以使用常量数组,但它不如“gangph”给出的解决方案灵活: (特别是必须在数组声明中给出数组的大小([0..1])。记录是不规则的,数组不是)


你的意思是没有明确声明所述记录的实例,不是吗?;)最好说“匿名记录初始化器”
type
  TRRec = record
    ident : string;
    classtype : TClass;
  end;

procedure Foo(AClasses : array of TRRec);
begin
end;

const tt: array [0..1] of TRRec = ((ident:'Button1'; classtype:TButton),
                                   (ident:'Lable1'; classtype:TLabel));

Begin
  Foo(tt);
end.