Delphi Error – E2058 Class, interface and object types only allowed in type section

Delphi Compiler Error

E2058 Class, interface and object types only allowed in type section

Reason for the Error & Solution

Class or object types must always be declared with an explicit type declaration in a type section – unlike record types, they cannot be anonymous.

The main reason for this is that there would be no way you could declare the methods of that type – after all, there is no type name.

program Produce;

var
  MyClass : class
    Field: Integer;
  end;

begin
end.

The example tries to declare a class type within a variable declaration – that is not legal.

program Solve;

type
  TMyClass = class
    Field: Integer;
  end;

var
  MyClass : TMyClass;

begin
end.

The solution is to introduce a type declaration for the class type. Alternatively, you could have changed the class type to a record type.

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