Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/9.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_Function_Record_Procedure - Fatal编程技术网

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

Delphi 如何使用记录定义和运行过程/函数?,delphi,function,record,procedure,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; ?这是可行的(参见代码中的语法注释): 虽然这很好,但并不是人们所要求的。(不是向下投票,顺便说一句。)海报问他们是否可以将过程run分配给Rec.p,然后使用

我想用过程或函数定义记录。 你能帮我学语法吗

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

procedure run;

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

procedure run;
begin
end;
可以稍后运行:

Rec[0].run;

这是可行的(参见代码中的语法注释):


虽然这很好,但并不是人们所要求的。(不是向下投票,顺便说一句。)海报问他们是否可以将
过程run
分配给
Rec.p
,然后使用
Rec.run
-您的代码显示为使用
Rec.p
,这与要求的不同。@KenWhite,您可能是对的,但我的示例允许OP通过const record Rec调用run过程。对,但问题并非如此,而是“以后可以运行:Rec[0].run;”,这是不可能的:没关系-OP似乎认为你的解决方案有效。(删除我的答案并对你的答案进行投票。)@KenWhite,我认为问题更多的是如何从语法的角度来实现这一点。显然你是对的(正如我所说,我对你的答案进行投票并删除了我的答案)。
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.