Delphi Compiler Error
E2018 Record, object or class type required
Reason for the Error & Solution
The compiler was expecting to find the type name which specified a record, object or class but did not find one.
program Produce;
type
RecordDesc = class
ch : Char;
end;
var
pCh : PChar;
r : RecordDesc;
procedure A;
begin
pCh.ch := 'A'; (* case 1 *)
with pCh do begin (* case 2 *)
end;
end;
end.
There are two causes for the same error in this program. The first is the application of ‘.’ to a object that is not a record. The second case is the use of a variable which is of the wrong type in a WITH statement.
program Solve;
type
RecordDesc = class
ch : Char;
end;
var
r : RecordDesc;
procedure A;
begin
r.ch := 'A'; (* case 1 *)
with r do begin (* case 2 *)
end;
end;
end.
The easy solution to this error is to always make sure that the ‘.’ and WITH are both applied only to records, objects or class variables.