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.