Board index » delphi » Stream an object to a TStringField

Stream an object to a TStringField

I found the following clip in Lloyd's help file.  I am however
very unfamiliar with streaming.  

Is there anyone who can tell me how to convert this code so that
it can be streamed to/from a normal TStringField in a Table?

--
Brahm van Niekerk
CEO - Elgin Data Development Corporation (PTY) Ltd.
P.O. Box 140, Elgin, South Africa
Tel. 027 83 270 2850, Fax 027 21 859 5028
mailto:br...@iafrica.com

<clip>
Q:   How do I save an object to a disk file?

A:  Use a stream to write to a disk file. The object must be a
component and is written to the stream like this:

 var Stream : TFileStream ;
 begin
   Stream := TFileStream.Create( 'AFile', fmCreate ) ;
   try
     Stream.WriteComponent( Button1 ) ;
     Stream.WriteComponent( Grid1 ) ; etc.
   finally
     Stream.Free ;
   end ;
 end ;

To read it back, do this:

 var Stream : TFileStream ;
     Button2 : TButton ;

     Grid2 : TStringGrid ;
 begin
   Stream := TFileStream.Create( 'AFile', fmOpenRead ) ;
   try
     Button2 := Stream.ReadComponent( nil ) as TButton ;
     Stream.WriteComponent( Grid1 ) ; etc.
   finally
     Stream.Free ;
   end ;
 end ;

At some point you need to register the classes you're going to
write and read. For example, you could put the following in the
forms OnCreate handler:

   RegisterClass( TButton ) ;
   RegisterClass( TStringGrid ) ;

If you don't register the classes you'll get a 'Class not found'
error when you try to read the object back.

{ This code came from Lloyd's help file! }

 

Re:Stream an object to a TStringField


Quote
Brahm van Niekerk wrote:

> I found the following clip in Lloyd's help file.  I am however
> very unfamiliar with streaming.

> Is there anyone who can tell me how to convert this code so that
> it can be streamed to/from a normal TStringField in a Table?

> --
> Brahm van Niekerk
> CEO - Elgin Data Development Corporation (PTY) Ltd.
> P.O. Box 140, Elgin, South Africa
> Tel. 027 83 270 2850, Fax 027 21 859 5028
> mailto:br...@iafrica.com

> <clip>
> Q:   How do I save an object to a disk file?

> A:  Use a stream to write to a disk file. The object must be a
> component and is written to the stream like this:

>  var Stream : TFileStream ;
>  begin
>    Stream := TFileStream.Create( 'AFile', fmCreate ) ;
>    try
>      Stream.WriteComponent( Button1 ) ;
>      Stream.WriteComponent( Grid1 ) ; etc.
>    finally
>      Stream.Free ;
>    end ;
>  end ;

> To read it back, do this:

>  var Stream : TFileStream ;
>      Button2 : TButton ;

>      Grid2 : TStringGrid ;
>  begin
>    Stream := TFileStream.Create( 'AFile', fmOpenRead ) ;
>    try
>      Button2 := Stream.ReadComponent( nil ) as TButton ;
>      Stream.WriteComponent( Grid1 ) ; etc.
>    finally
>      Stream.Free ;
>    end ;
>  end ;

> At some point you need to register the classes you're going to
> write and read. For example, you could put the following in the
> forms OnCreate handler:

>    RegisterClass( TButton ) ;
>    RegisterClass( TStringGrid ) ;

> If you don't register the classes you'll get a 'Class not found'
> error when you try to read the object back.

> { This code came from Lloyd's help file! }

Perhaps it's really TBlobField you should use to store the state of
arbitrary components (with Paradox you only get 255 characters in a
string field of a table). Example:

var
  ms: TMemoryStream;
begin
  ms := TMemoryStream.Create;
  try
    ms.WriteComponent(Button1);
    { May need

    ms.Position := 0;

    here.}
    BlobField.Assign(ms);
  finally
    ms.Free;
  end;
end;

Cheers,
Graham Meintjes

Other Threads