Delphi Error – E2094 Local procedure/function ‘%s’ assigned to procedure variable

Delphi Compiler Error

E2094 Local procedure/function ‘%s’ assigned to procedure variable

Reason for the Error & Solution

This error message is issued if you try to assign a local procedure to a procedure variable, or pass it as a procedural parameter.

This is illegal, because the local procedure could then be called even if the enclosing procedure is not active. This situation would cause the program to crash if the local procedure tried to access any variables of the enclosing procedure.

program Produce;

var
  P: Procedure;

procedure Outer;

  procedure Local;
  begin
    Writeln('Local is executing');
  end;

begin
  P := Local;       (*<-- Error message here*)
end;

begin
  Outer;
  P;
end.

The example tries to assign a local procedure to a procedure variable. This is illegal because it is unsafe at run time.

program Solve;

var
  P: Procedure;

procedure NonLocal;
begin
  Writeln('NonLocal is executing');
end;

procedure Outer;

begin
  P := NonLocal;
end;

begin
  Outer;
  P;
end.

The solution is to move the local procedure out of the enclosing one.

Share:

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

Delphi Compiler Error E2313 Attribute – Known attribute cannot specify properties Reason for the Error & Solution No further information...
Delphi Compiler Error E2379 Virtual methods not allowed in record types Reason for the Error & Solution No further information...
Rodrigo , one of the long time Delphi Developer has been working on one of his personal project “Delphi IDE...