Subclass array of a superclass in Delphi XE7

Asked

Viewed 52 times

0

I am trying to make an array of subclasses in this format:

 arrayReg : Array[0..1] of TRegDom = (TRegDom0000); 

Tregdom0000 being a subclass of Tregdom (Tregdom0000(Tregdom)) and gives an error of "Incompatible types E2010":

'Tregdom' and 'class of Tregdom0000'

  • I couldn’t quite understand what you want to do... if you want to initialize the array, I believe you have to put an instance of the class where you placed the Tregdom0000 class

  • I need this array so I can go through all the Tregdom descendants, all methods are like class functions / procedures, because I don’t need to instantiate them. I wanted an array with all subclasses, I do it in JAVA exactly this way with superclass and subclass but not working in Delphi.

  • I may be wrong but I believe that it is not possible to create this array in Delphi/Pascal, probably the way to do it should be totally different

  • Doing this by initializing the array will not do it. But I think you can do it in the code body, with a cast

1 answer

0

You have to declare types for arrays in order to cast. Something like

MyParentClass = class
end;

MyChildClass = class(MyParentClass)
end;

TArrayOFMyParentClass = array of MyParentClass;
TArrayOFMyChildClass = array of MyChildClass;

And then in the code you can do

procedure TForm6.FormCreate(Sender: TObject);
var
  parentArr: TArrayOFMyParentClass;
  childArr: TArrayOFMyChildClass;
begin
  setlength(childArr, 20);

  parentArr := TArrayOFMyParentClass(childArr);
end;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.