Delphi – E2060 Class and interface types only allowed in type section

In a type section of your delphi program, class or interface types must always be stated with an explicit type declaration. They cannot be anonymous, unlike other record kinds.

The key reason for this is that you wouldn’t be able to declare the type’s methods otherwise (since there is no type name).

Attempting to declare a class type within a variable declaration is incorrect:

program Produce;

var
  MyClass : class
    Field: Integer;
  end;

begin
end.

How to fix it?

program Solve;

type
  TMyClass = class
    Field: Integer;
  end;

var
  MyClass : TMyClass;

begin
end.
%d bloggers like this: