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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:33:16+00:00 2026-06-18T14:33:16+00:00

I’m getting very confused about how to write out properties from a TComponent that

  • 0

I’m getting very confused about how to write out properties from a TComponent that has a TPersistent field. For example I have:

  TChildObj = class( TPersistent )
  PRIVATE
    FVisible: boolean;
    FColor: TColor;
  PUBLIC
  PUBLISHED
    property Visible : boolean
               read FVisible
               write FVisible;
    property Color : TColor
               read FColor
               write FColor;
  end;


  TTest = class( TComponent )
    constructor Create( AOwner : TComponent ); override;
    destructor Destroy; override;
  private
    FChildObj : TChildObj;
    FOne: integer;
  published
    property One : integer
               read FOne
               write FOne;
    property ChildObj : TChildObj
               read FChildObj;
  end;

When I use the following writer code:

procedure TForm1.Button5Click(Sender: TObject);
var
  MS : TMemoryStream;
  SS : TStringStream;
  Test : TTest;
begin
  Test := TTest.Create( Self );
  MS := TMemoryStream.Create;
  SS := TStringStream.Create;
  try
    MS.WriteComponent( Test );
    MS.Position := 0;
    ObjectBinaryToText( MS, SS );
    SS.SaveToFile( 'c:\scratch\test.txt' );
  finally
    MS.Free;
    SS.Free;
  end;

end;

I get only the following:

object TTest
  One = 0
end

i.e the TPersistent TChildObj is missing.

This article on component seriealization states “A Component will stream by default any property of type TPersistent that is not a TComponent. Our TPersistent property is streamed just like a component, and it may have other TPersistent properties that will get streamed.” however when I step into System.Classes, at around line 12950 (XE3) there is the test:

  if (PropInfo^.GetProc <> nil) and
     ((PropInfo^.SetProc <> nil) or
     ((PropInfo^.PropType^.Kind = tkClass) and
      (TObject(GetOrdProp(Instance, PropInfo)) is TComponent) and
      (csSubComponent in TComponent(GetOrdProp(Instance, PropInfo)).ComponentStyle))) then

which seems to indicate that only components and sub-components are serialised. If I make TChildObj descend from TComponent (and give it a name) I get its name appearing in the written file (but still no properties).

What I really dont understand is that TControl (a component) has the Font property (TPersistent) and this gets streamed out fine when you write a TLabel for example.

Or is this something to do with default properties?

Any help appreciated.

  • 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-06-18T14:33:18+00:00Added an answer on June 18, 2026 at 2:33 pm

    Look more closely at the list of requirements when the RTL is deciding if it needs to stream a TPersistent property:

    if (PropInfo^.GetProc <> nil) and
     ((PropInfo^.SetProc <> nil) or
     ((PropInfo^.PropType^.Kind = tkClass) and
      (TObject(GetOrdProp(Instance, PropInfo)) is TComponent) and
      (csSubComponent in TComponent(GetOrdProp(Instance, PropInfo)).ComponentStyle))) then
    

    Your ChildObj property is a read-only property, so it does not satisfy the PropInfo^.SetProc <> nil requirement, and it is not a TComponent-derived sub-component, so it does not satisfy the is is TComponent and csSubComponent requirements. That is why your property is missing from the DFM.

    The simpliest solution is to make your ChildObj property be read/write instead of read-only (don’t use TComponent unless you have to, which you don’t in this situation).

    You are also missing a destructor in TTest to free the TChildObj object. And for good measure, you should give TChildObj an OnChange event that TTest can assign a handler to, so it can react to changes to the TChildObj sub-properties.

    Try this:

    type
      TChildObj = class(TPersistent)
      private
        FVisible : Boolean;
        FColor : TColor;
        FOnChange : TNotifyEvent;
        procedure Changed;
        procedure SetVisible(Value : Boolean);
        procedure SetColor(Value : TColor);
      public
        procedure Assign(Source : TPersistent); override;
        property OnChange : TNotifyEvent read FOnChange write FOnChange;
      published
        property Visible : Boolean read FVisible write SetVisible;
        property Color : TColor read FColor write SetColor;
      end;
    
      TTest = class(TComponent)
      private
        FChildObj : TChildObj;
        FOne : integer;
        procedure ChildObjChanged(Sender : TObject);
        procedure SetChildObj(Value : TChildObj);
      protected
        procedure Loaded; override;
      public
        constructor Create(AOwner : TComponent); override;
        destructor Destroy; override;
      published
        property One : integer read FOne write FOne;
        property ChildObj : TChildObj read FChildObj write SetChildObj;
      end;
    

    .

    procedure TChildObj.Assign(Source: TPersistent);
    begin
      if Source is TChildObj then
      begin
        FVisible := TChildObj(Source).Visible;
        FColor := TChildObj(Source).Color;
        Changed;
      end else
        inherited;
    end;
    
    procedure TChildObj.Changed;
    begin
      if Assigned(FOnChange) then
        FOnChange(Self);
    end;
    
    procedure TChildObj.SetVisible(Value : Boolean);
    begin
      if FVisible <> Value then
      begin
        FVisible := Value;
        Changed;
      end;
    end;
    
    procedure TChildObj.SetColor(Value : TColor);
    begin
      if FColor <> Value then
      begin
        FColor := Value;
        Changed;
      end;
    end;
    
    constructor TTest.Create(AOwner : TComponent);
    begin
      inherited;
      FChildObj := TChildObj.Create;
      FChildObj.OnChange := ChildObjChanged;
    end;
    
    destructor TTest.Destroy;
    begin
      FChildObj.Free;
      inherited;
    end;
    
    procedure TTest.ChildObjChanged(Sender : TObject);
    begin
      if csLoading in ComponentState then Exit;
      // use ChildObj values as needed...
    end;
    
    procedure TTest.Loaded;
    begin
      inherited;
      ChildObjChanged(nil);
    end;
    
    procedure TTest.SetChildObj(Value : TChildObj);
    begin
      if FChildObj <> Value then
        FChildObj.Assign(Value);
    end;
    

    If you go the TComponent approach, then try this instead:

    type
      TChildObj = class(TComponent)
      private
        FVisible : Boolean;
        FColor : TColor;
        FOnChange : TNotifyEvent;
        procedure Changed;
        procedure SetVisible(Value : Boolean);
        procedure SetColor(Value : TColor);
      public
        procedure Assign(Source : TPersistent); override;
        property OnChange : TNotifyEvent read FOnChange write FOnChange;
      published
        property Visible : Boolean read FVisible write SetVisible;
        property Color : TColor read FColor write SetColor;
      end;
    
      TTest = class(TComponent)
      private
        FChildObj : TChildObj;
        FOne : integer;
        procedure ChildObjChanged(Sender : TObject);
        procedure SetChildObj(Value : TChildObj);
      protected
        procedure Loaded; override;
      public
        constructor Create(AOwner : TComponent); override;
      published
        property One : integer read FOne write FOne;
        property ChildObj : TChildObj read FChildObj write SetChildObj;
      end;
    

    .

    procedure TChildObj.Assign(Source: TPersistent);
    begin
      if Source is TChildObj then
      begin
        FVisible := TChildObj(Source).Visible;
        FColor := TChildObj(Source).Color;
        Changed;
      end else
        inherited;
    end;
    
    procedure TChildObj.Changed;
    begin
      if Assigned(FOnChange) then
        FOnChange(Self);
    end;
    
    procedure TChildObj.SetVisible(Value : Boolean);
    begin
      if FVisible <> Value then
      begin
        FVisible := Value;
        Changed;
      end;
    end;
    
    procedure TChildObj.SetColor(Value : TColor);
    begin
      if FColor <> Value then
      begin
        FColor := Value;
        Changed;
      end;
    end;
    
    constructor TTest.Create(AOwner : TComponent);
    begin
      inherited;
      FChildObj := TChildObj.Create(Self);
      FChildObj.SetSubComponent(True);
      FChildObj.OnChange := ChildObjChanged;
    end;
    
    procedure TTest.ChildObjChanged(Sender : TObject);
    begin
      if csLoading in ComponentState then Exit;
      // use ChildObj values as needed...
    end;
    
    procedure TTest.Loaded;
    begin
      inherited;
      ChildObjChanged(nil);
    end;
    
    procedure TTest.SetChildObj(Value : TChildObj);
    begin
      if FChildObj <> Value then
        FChildObj.Assign(Value);
    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
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I know there's a lot of other questions out there that deal with this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am confused How to use looping for Json response Array in another Array.
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but

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.