elector
elector

Reputation: 1339

Delphi - TInterfaceList descendant to support more types - Abstract Error

I have a class TRow = class(TInterfaceList) where the items I want to add are descendants from IField (TFieldType1 and TFieldType2) I have a method where I want to iterate through the items and call an IField method on them:

(Item[i] as IField).IFieldMethod

And I am getting Abstract Error? What am I doing wrong? Am I wrong in my OOP understanding or Delphi?

Thank you

Upvotes: 0

Views: 410

Answers (1)

Cosmin Prund
Cosmin Prund

Reputation: 25678

You've got a class implementing IField.IFieldMethod using a abstract virtual method, and you ignored a Constructing instance of <TType> containing abstract method <MethodName>.

Here's a short console demo of code that exhibits your error. The fact that you're calling IFieldMethod using (Item[i] as IField).IFieldMethod doesn't provide any new information, it only hides the cause of the problem. Store the IField type interface reference to a local variable and you've got my code:

program Project23;

{$APPTYPE CONSOLE}

uses
  SysUtils,
  Classes;

type IDummyIntf = interface
  procedure DoStuff;
end;

type TDummyImp = class(TInterfacedObject, IDummyIntf)
  procedure DoStuff;virtual;abstract; // TDummyImp implements IDummyIntf.DoStuff using a VIRTUAL ABSTRACT method.
end;

var X: IDummyIntf;
begin
  X := TDummyImp.Create; // <-- Warning at this line, Constructing instance of TDummyImp containing abstract method TDummyImp.DoStuff 
  X.DoStuff; // This raises EAbstractError because TDummyImp doesn't actually implement DoStuff
  ReadLn;
end.

Upvotes: 1

Related Questions