Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2002-12

TWriter.WriteProperties

2 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Judison

Hi,

I have to write some instances of a TA class(TPersistent) in a property of TB class(TComponent)... I mean:

object MyB: TB
...
Items = <
item
Caption = 'Hello'
ImageIndex = 3

  end
  item
    Caption = 'Bye'
    ImageIndex = 4
  end
end

But I can't (don't want) use TColletion/TCollectionItem in this specific case... I have it working now, but I need to use a bad programming trick because I use the WriteProperties method of TWriter that is protected, so, I have a

type
  TFakeWriter = class(TWriter)
  end;
  TFakeReader = class(TReader)
  end;

Just to use use this method by casting the Writer (and the Reader for ReadProperty)

I don't like to do this kind of thing
and.. I my mother see it, I'm a dead guy ;)

My WriteData method looks like this:

procedure TB.WriteData(Writer: TWriter);
var
  I: Integer;
begin
  Writer.Driver.BeginCollection;
  for i := 0 to ItemCount - 1 do
  begin
    Writer.Driver.BeginList;
    TFakeWriter(Writer).WriteProperties(GetItem(I));
    Writer.Driver.EndList;
  end;
  Writer.Driver.EndList;
end;

There is other way to do this?
I think about a lot of BeginProperty / WriteXXX / EndProperty but if I do this, I'll have problems creating another class(TA), if I add some properties to this new class.

I hope you understand my bad english.... :)

TIA
Your friend,
Judison

Reply by email

Unknown author
to Judison
Judison wrote 51 lines
Hi, I have to write some instances of a TA class(TPersistent) in a property of TB class(TComponent)... I mean: object MyB: TB ... Items = < item Caption = 'Hello' ImageIndex = 3 end item Caption = 'Bye' ImageIndex = 4 end > end But I can't (don't want) use TColletion/TCollectionItem in this specific case... I have it working now, but I need to use a bad programming trick because I use the WriteProperties method of TWriter that is protected, so, I have a type TFakeWriter = class(TWriter) end; TFakeReader = class(TReader) end; Just to use use this method by casting the Writer (and the Reader for ReadProperty) I don't like to do this kind of thing and.. I my mother see it, I'm a dead guy ;) My WriteData method looks like this: procedure TB.WriteData(Writer: TWriter); var I: Integer; begin Writer.Driver.BeginCollection; for i := 0 to ItemCount - 1 do begin Writer.Driver.BeginList; TFakeWriter(Writer).WriteProperties(GetItem(I)); Writer.Driver.EndList; end; Writer.Driver.EndList; end; There is other way to do this? I think about a lot of BeginProperty / WriteXXX / EndProperty but if I do this, I'll have problems creating another class(TA), if I add some properties to this new class.

The proper thing to do is to use the DefineProperties call, and use that to read/write your custom data.

Michael.

Reply by email