Delphi Error – E2096 Method identifier expected

Delphi Compiler Error

E2096 Method identifier expected

Reason for the Error & Solution

This error message will be issued in several different situations:

  • Properties in an automated section must use methods for access, they cannot use fields in their read or write clauses.
  • You tried to call a class method with the “ClassType.MethodName” syntax, but “MethodName” was not the name of a method.
  • You tried calling an inherited with the “Inherited MethodName” syntax, but “MethodName” was not the name of a method.

program Produce;

type
  TMyBase = class
    Field: Integer;
  end;
  TMyDerived = class (TMyBase)
    Field: Integer;
    function Get: Integer;
  Automated
    property Prop: Integer read Field;    (*<-- Error message here*)
  end;

function TMyDerived.Get: Integer;
begin
  Result := TMyBase.Field;                (*<-- Error message here*)
end;

begin
end.

The example tried to declare an automated property that accesses a field directly. The second error was caused by trying to get at a field of the base class – this is also not legal.

program Solve;

type
  TMyBase = class
    Field: Integer;
  end;
  TMyDerived = class (TMyBase)
    Field: Integer;
    function Get: Integer;
  Automated
    property Prop: Integer read Get;
  end;

function TMyDerived.Get: Integer;
begin
  Result := TMyBase(Self).Field;
end;

begin
  Writeln( TMyDerived.Create.Prop );
end.

The first problem is fixed by accessing the field via a method. The second problem can be fixed by casting the Self pointer to the base class type, and accessing the field off of that.

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...