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 X2421 Imported identifier ‘%s’ conflicts with ‘%s’ in ‘%s’ Reason for the Error & Solution This occurs...
Delphi Compiler Error X2367 Case of property accessor method %s.%s should be %s.%s Reason for the Error & Solution No...
Delphi Compiler Error X2269 Overriding virtual method ‘%s.%s’ has lower visibility (%s) than base class ‘%s’ (%s) Reason for the...