如何使用记录定义和运行过程/函数?

时间:2012-03-23 23:46:06

标签: delphi function record procedure

我想用程序或功能定义记录。 你能帮助语法吗?

Type TRec = record
 s: string;
 p: procedure;
end;

procedure run;

Const
  Rec: TRec = ('',run);

procedure run;
begin
end;

以后可以运行:

Rec[0].run;

1 个答案:

答案 0 :(得分:1)

这有效(参见代码中的语法注释):

Type
  TRec = record
    s: string;
    p: procedure; // As Ken pointed out, better define a procedural type:
                  //  type TMyProc = procedure; and declare p : TMyProc;
  end;

procedure run; forward;  // The forward is needed here.
                         // If the procedure run was declared in the interface
                         // section of a unit, the forward directive should not be here.

Const
  Rec: TRec = (s:''; p:run);  // The const record is predefined by the compiler.

procedure run;
begin
  WriteLn('Test');
end;

begin
  Rec.p;  // Rec.run will not work since run is not a declared member of TRec.
          // The array index (Rec[0]) is not applicable here since Rec is not declared as an array.
  ReadLn;
end.