Hi All,

I am very new to DELPHI PASCAL Programming.
I have imported the TLB file using Component->Import Component. I would like to use the class of the component and call its method.

for that i have declared class object like
type
TSam1 = class(SampleClass)
end;

and called the method of it as

TSam1.DisplayResult();

I am getting "E2076 This form of method call only allowed for class methods." Can anyone please guide me, where i am going wrong.

Thankyou,

Dani AI

Generated

As explained, E2076 means the method being called is an instance method, not a class method — calling it using the type name is only valid for methods declared with the class keyword. Declaring a second class identical to the imported one is unnecessary; the import unit already exposes the usable types.

When a TLB is imported Delphi usually produces one of two typical results:

  • An interface + CoClass factory (the import unit, normally named <LibraryName>_TLB.pas, will contain interface declarations and a CoSomething helper with Create/CreateRemote that return the interface). Example usage pattern with the generated factory:

    var
      Obj: IMySample;
    begin
      Obj := CoMySample.Create;   // returns an interface
      Obj.ShowResult;
    end;

    Interfaces are reference-counted in Delphi, so they do not require an explicit Free.

  • A VCL wrapper component (for ActiveX controls), in which case the import produces a T... component class that should be instantiated like any other VCL component (or dropped on a form). That kind of class must be created and owned/freed explicitly (owner can manage lifetime).

Practical troubleshooting steps: open the generated import unit to see whether the type is an interface (look for interface and Co-prefixed factory) or a T... class; check the method signature (instance vs class); avoid needless subclassing that only renames the original type. If instantiation fails at runtime, ensure COM is initialized for the thread (use the usual COM/OLE initialization calls when appropriate). These checks will make it clear which instantiation pattern to use and why the E2076 error appeared.

Recommended Answers

All 2 Replies

i'm not sure what TLB is..can you give more details about this?

The problem is that you are referring to the TYPE TSam1 when you call your method. That is only possible if the method is a class method (and if you're a new student, you won't have heard about class methods yet). What you need to do is declare a variable of the type TSam1, create an instance of that type and assign it to the variable and then use the variable to call your method:

var
mySam :TSam1
begin
mySam := TSam1.Create;
mySam.DisplayResult;
end;

PS- why are you declaring TSam1 at all? The way your code reads, it is exactly the same as SampleClass and is unnecessary. Maybe you were attempting to create an instance of SampleClass named TSam1, in which case your syntax was incorrect.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.