Delphi Error – E2076 This form of method call only allowed for class methods

Delphi Compiler Error

E2076 This form of method call only allowed for class methods

Reason for the Error & Solution

You were trying to call a normal method by just supplying the class type, not an actual instance.

This is only allowed for class methods and constructors, not normal methods and destructors.

program Produce;

type
  TMyClass = class
  (*...*)
  end;
var
  MyClass: TMyClass;

begin
  MyClass := TMyClass.Create;  (*Fine, constructor*)
  Writeln(TMyClass.ClassName); (*Fine, class method*)
  TMyClass.Destroy;            (*<-- Error message here*)
end.

The example tries to destroy the type TMyClass – this doesn’t make sense and is therefore illegal.

program Solve;
type
  TMyClass = class
  (*...*)
  end;
var
  MyClass: TMyClass;

begin
  MyClass := TMyClass.Create;  (*Fine, constructor*)
  Writeln(TMyClass.ClassName); (*Fine, class method*)
  MyClass.Destroy;             (*Fine, called on instance*)
end.

As you can see, we really meant to destroy the instance of the type, not the type itself.

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