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 X2421 Imported identifier ‘%s’ conflicts with ‘%s’ in ‘%s’ Reason for the Error & Solution This occurs...
Delphi Compiler Error X2367 Case of property accessor method %s.%s should be %s.%s Reason for the Error & Solution No...
Delphi Compiler Error X2269 Overriding virtual method ‘%s.%s’ has lower visibility (%s) than base class ‘%s’ (%s) Reason for the...