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 6229757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:37:41+00:00 2026-05-24T09:37:41+00:00

I’ve got my custom collection property which is working great when it is a

  • 0

I’ve got my custom collection property which is working great when it is a direct member of my component.

But I want to move the collection property to a TPersistent propery within my component. And now comes the problem, it doesn’t work: double clicking on the collection property in the object inspector normally opens the collection editor, but it does not anymore.

Fist of all – what should I pass to the contructor of the TPersistent property?

TMyCollection = class(TCollection)
  constructor Create(AOwner: TComponent); // TMyCollection constuctor
  ...

I can’t pass Self, so should I pass my persistent owner?

constructor TMyPersistent.Create(AOwner: TComponent);
begin
  inherited Create;
  fOwner := AOwner;
  fMyCollection := TMyCollection.Create(AOwner); // hmmm... doesn't make sense
end;

I think I’m missing something. If more code is needed just please comment this post.

Da visualizationz

  • 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-24T09:37:41+00:00Added an answer on May 24, 2026 at 9:37 am

    A TCollection’s constructor does not need a TComponent, but a TCollectionItemClass.

    Your collection now being a member of a TPersistent property instead of being a direct member of the component makes no difference for the constructor.


    Update

    What dóes differ is the ownership, but then at the TPersistent level, which should be managed by a correct implementation of GetOwner:

    GetOwner returns the owner of an object. GetOwner is used by the GetNamePath method to find the owner of a persistent object. GetNamePath and GetOwner are introduced in TPersistent so descendants such as collections can appear in the Object Inspector.

    You have to tell the IDE that your TCollection property is owned by the TPersistent property, which in turn is owned by the component.

    The tutorial you are using has several errors regarding this implementation:

    • The owner of the collection is declared as TComponent, which should be TPersistent,
    • GetOwner is not implemented for the TPersistent property class, and
    • The fix shown at the end of the tutorial, stating that the TPersistent property should inherit from TComponent instead, is plain wrong; or more nicely said: is rather a workaround for not implementing GetOwner.

    This is how it should look like:

    unit MyComponent;
    
    interface
    
    uses
      Classes, SysUtils;
    
    type
      TMyCollectionItem = class(TCollectionItem)
      private
        FStringProp: String;
      protected
        function GetDisplayName: String; override;
      public
        procedure Assign(Source: TPersistent); override;
      published
        property StringProp: String read FStringProp write FStringProp;
      end;
    
      TMyCollection = class(TCollection)
      private
        FOwner: TPersistent;
        function GetItem(Index: Integer): TMyCollectionItem;
        procedure SetItem(Index: Integer; Value: TMyCollectionItem);
      protected
        function GetOwner: TPersistent; override;
      public
        constructor Create(AOwner: TPersistent);
        function Add: TMyCollectionItem;
        function Insert(Index: Integer): TMyCollectionItem;
        property Items[Index: Integer]: TMyCollectionItem read GetItem
          write SetItem;
      end;
    
      TMyPersistent = class(TPersistent)
      private
        FOwner: TPersistent;
        FCollectionProp: TMyCollection;
        procedure SetCollectionProp(Value: TMyCollection);
      protected
        function GetOwner: TPersistent; override;
      public
        procedure Assign(Source: TPersistent); override;
        constructor Create(AOwner: TPersistent);
        destructor Destroy; override;
      published
        property CollectionProp: TMyCollection read FCollectionProp
          write SetCollectionProp;
      end;
    
      TMyComponent = class(TComponent)
      private
        FPersistentProp: TMyPersistent;
        procedure SetPersistentProp(Value: TMyPersistent);
      public
        constructor Create(AOwner: TComponent); override;
        destructor Destroy; override;
      published
        property PersistentProp: TMyPersistent read FPersistentProp
          write SetPersistentProp;
      end;
    
    procedure Register;
    
    implementation
    
    procedure Register;
    begin
      RegisterComponents('Samples', [TMyComponent]);
    end;
    
    { TMyCollectionItem }
    
    procedure TMyCollectionItem.Assign(Source: TPersistent);
    begin
      if Source is TMyCollectionItem then
        FStringProp := TMyCollectionItem(Source).FStringProp
      else
        inherited Assign(Source);
    end;
    
    function TMyCollectionItem.GetDisplayName: String;
    begin
      Result := Format('Item %d',[Index]);
    end;
    
    { TMyCollection }
    
    function TMyCollection.Add: TMyCollectionItem;
    begin
      Result := TMyCollectionItem(inherited Add);
    end;
    
    constructor TMyCollection.Create(AOwner: TPersistent);
    begin
      inherited Create(TMyCollectionItem);
      FOwner := AOwner;
    end;
    
    function TMyCollection.GetItem(Index: Integer): TMyCollectionItem;
    begin
      Result := TMyCollectionItem(inherited GetItem(Index));
    end;
    
    function TMyCollection.GetOwner: TPersistent;
    begin
      Result := FOwner;
    end;
    
    function TMyCollection.Insert(Index: Integer): TMyCollectionItem;
    begin
      Result := TMyCollectionItem(inherited Insert(Index));
    end;
    
    procedure TMyCollection.SetItem(Index: Integer; Value: TMyCollectionItem);
    begin
      inherited SetItem(Index, Value);
    end;
    
    { TMyPersistent }
    
    procedure TMyPersistent.Assign(Source: TPersistent);
    begin
      if Source is TMyPersistent then
        CollectionProp := TMyPersistent(Source).FCollectionProp
      else
        inherited Assign(Source);
    end;
    
    constructor TMyPersistent.Create(AOwner: TPersistent);
    begin
      inherited Create;
      FOwner := AOwner;
      FCollectionProp := TMyCollection.Create(Self);
    end;
    
    destructor TMyPersistent.Destroy;
    begin
      FCollectionProp.Free;
      inherited Destroy;
    end;
    
    function TMyPersistent.GetOwner: TPersistent;
    begin
      Result := FOwner;
    end;
    
    procedure TMyPersistent.SetCollectionProp(Value: TMyCollection);
    begin
      FCollectionProp.Assign(Value);
    end;
    
    { TMyComponent }
    
    constructor TMyComponent.Create(AOwner: TComponent);
    begin
      inherited Create(AOwner);
      FPersistentProp := TMyPersistent.Create(Self);
    end;
    
    destructor TMyComponent.Destroy;
    begin
      FPersistentProp.Free;
      inherited Destroy;
    end;
    
    procedure TMyComponent.SetPersistentProp(Value: TMyPersistent);
    begin
      FPersistentProp.Assign(Value);
    end;
    
    end.
    

    But may I say that you can also inherit from TOwnedCollection, which makes the use and the declaration of TMyCollection much simpler:

      TMyCollection = class(TOwnedCollection)
      private
        function GetItem(Index: Integer): TMyCollectionItem;
        procedure SetItem(Index: Integer; Value: TMyCollectionItem);
      public
        function Add: TMyCollectionItem;
        function Insert(Index: Integer): TMyCollectionItem;
        property Items[Index: Integer]: TMyCollectionItem read GetItem
          write SetItem;
      end;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
I have a French site that I want to parse, but am running into
I want to construct a data frame in an Rcpp function, but when I
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I've got a string that has curly quotes in it. I'd like to replace
Seemingly simple, but I cannot find anything relevant on the web. What is the
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

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.