How to define and run procedure/function using record?

I want to define record with procedure or function. Can you help with syntax?

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

procedure run;

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

procedure run;
begin
end;

It's possible to run later:

Rec[0].run;

?

Upvotes: 0

Views: 3007

Answers (1)

LU RD
LU RD

Reputation: 34889

This works (see syntax comments in code):

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.

Upvotes: 1

Related Questions