문제

레코드를 메소드 매개 변수로 사용하고 해당 레코드 인스턴스를 암시 적으로 선언하지 않고 호출 할 수 있습니까?

이와 같은 코드를 작성하고 싶습니다.

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

procedure Foo(AClasses : array of TRRec);

그런 다음 방법을 이와 같은 또는 비슷한 것을 호출하십시오.

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

그건 그렇고 나는 여전히 델파이 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)]);

다른 팁

Const 배열에서도 작업 할 수 있지만 "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.
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top