1

Can somebody please tell me why I am getting the

Incompatible type: 'TChild' and 'Class of TChild'

 TBase = class (TObject)
 end;

 TMyList<T: TBase> = class(TObjectList<T>)
 end;

The error comes when I declare a child from the base class and try to create a list of TChild.

TChild = class (TBase)
end;

TChildList = TMyList<TChild>;
2
  • 5
    I don't think you've shown the real code. Please show SSCCE. Please also ask one question at a time. Commented May 15, 2014 at 5:39
  • I dont get any errors here. I litterally copied the code you posted. Added a property to TBase called Id, made a Tchildlist and added 4 tchilds. No proplems at all. Commented Jun 12, 2014 at 10:57

1 Answer 1

4

The code in the question is fine. Here is proof, a complete program that compiles:

program Project1;

{$APPTYPE CONSOLE}

uses
  System.Generics.Collections;

type
  TBase = class (TObject)
  end;

  TMyList<T: TBase> = class(TObjectList<T>)
  end;

  TChild = class (TBase)
  end;

  TChildList = TMyList<TChild>;

begin
end.

Clearly you need to edit the question to post code that exhibits the fault. Feel free to use the above as a template for how to provide a Delphi language SSCCE.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks at-least your example has showed me the error was not in the place that i was thinking it to be. The compiler was giving error in a different place. I wanted to know the type of data that my list class holds and I was returning the type as a property. type TBaseList<T: TBaseClass> = class(TObjectList<T>) private function get__Type: T; public property _Type: T read get__Type; end; this was causing the error. If i remove this property then the code works fine. Do you know how i can get the class type that a list holds.
I'd be happy to answer this question if you asked it as a question. It seems clear what is wrong. The property has type T. That means the property holds an instance of type T. You would need the property to be of type class of T to return a meta class. I showed you in my answer how to write an SSCCE. Please do follow that lead and ask your question as either a new question or an edit, but giving a proper SSCCE.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.