[fpc-devel]TWriter.WriteProperties

Michael.VanCanneyt at Wisa.be Michael.VanCanneyt at Wisa.be
Wed Dec 18 09:43:35 CET 2002


On Wed, 18 Dec 2002, Judison wrote:

> 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.





More information about the fpc-devel mailing list