Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 972813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:10:55+00:00 2026-05-16T03:10:55+00:00

I’m not sure if this is possible in Delphi. I’ve looked around and can’t

  • 0

I’m not sure if this is possible in Delphi. I’ve looked around and can’t seem to find an answer (Example or inidaction that it’s not possible):

I have a generic list class, and I want to create an instance of the generic type. For example:

type
  TTypeA = class(TObject);

procedure Test;
var
  MyList: TobjectList<TTypeA>;
  NewListObject: TTypeA;
begin
  MyList := TObjectList<TTypeA>.Create;
  NewListObject := MyList.xxx //what to put on the xxx
end;

Is it possible to create a function xxx that creates a new object of the type TTypeA?

@jeroen: thanks for the answer below. However, I forgot an important detail in my question:

I would like this code to work for any other type as well, so without prior knowledge about the type T for TObjectList. I might create the following lists:

MyList: TObjectList<TCar>;
MyList: TObjectList<TBike>;

Without knowing if MyList contains TCar or TBike (both derived from the same base class and equal constructors) I want to add a new item to MyList.

And with the suggestion from Uwe Raabe I run into the next problem:

I modified my class to

TMyObjectList<T:class, constructor> = class(TMyBaseObjectList<T>)

where TMyBaseObjectList is defined as

TMyBaseObjectList<T:TMyBaseObject> = class(TObjectList)

Now I get an error:
Type parameter ‘T’ is not compatible with type ‘T:TMyBaseObject’

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T03:10:56+00:00Added an answer on May 16, 2026 at 3:10 am

    As you know the type, why don’t you just write

    NewListObject := TTypeA.Create;
    MyList.Add(NewListObject);
    

    It would be the natural way to do here. (I’m just notified by Jeroen’s similar answer)

    If you want the container create the object, you have to make a descendant class that knows a little bit more about the generic type to create an instance. The constructor constraint might help here.

    type
      TMyObjectList<T:class, constructor> = class(TObjectList<T>)
      public
        function NewObject: T;
      end;
    
    function TMyObjectList<T>.NewObject: T;
    begin
      result := T.Create;
    end;
    

    Note: This will only work when the actual type has a parameterless constructor named Create.

    Update: This will help

      TMyObjectList<T:constructor, TMyBaseObject> = class(TObjectList<T>)
      public
        function NewObj: T;
      end;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm not entirely sure how I managed to jack this up. http://pretty-senshi.com If you
I know there's a lot of other questions out there that deal with this
I need a function that will clean a strings' special characters. I do NOT
Does anyone know how can I replace this 2 symbol below from the string
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.