Delphi Language - Did you know#

Did you know that all Delphi streams automatically support persisting/loading components, just as the IDE uses it? While looking today how again to persist a component state, I came across the methods WriteComponent and ReadComponent. I honestly never saw those before. Therefore persisting a component into a stream is just one line, when you want to have the data in text form, simply use ObjectBinaryToText and ObjectTextToBinary for the transformations. Great, how easy life can be. This method works fo published properties only, but most often this will do just fine.

// code to stream component state to file
FileStream := TFileStream.Create(Filename, fmCreate); 
BinStream := TMemoryStream.Create;
try
  BinStream.WriteComponent(YourComponent);
  BinStream.Position := 0;
  ObjectBinaryToText(BinStream, FileStream);
finally
  BinStream.Free;
  FileStream.Free;
end;

// code to load component streamed component from file
FileStream := TFileStream.Create(Filename, fmOpenRead);
BinStream := TMemoryStream.Create;
try
  ObjectTextToBinary(FileStream, BinStream);
  BinStream.Position := 0;
  BinStream.ReadComponent(YourComponent);
finally
  BinStream.Free;
  FileStream.Free;
end;

As a note, I am sure, I knew this before as I have done that a few years ago already, but hey, sometimes you just forget and rediscover and you are happy again...

Tuesday, October 09, 2007 4:01:39 PM (W. Europe Standard Time, UTC+01:00) #    Comments [6]  | 

 

Google AdSense


Wednesday, October 10, 2007 3:47:26 AM (W. Europe Standard Time, UTC+01:00)
This is a very quick way to store application options.

Have used it in a few applications, although never stored them as text - customers might think they can modify the file directly! Then when they mess it up, they won't be blaming themselves <g>
Wednesday, October 10, 2007 5:02:45 PM (W. Europe Standard Time, UTC+01:00)
Your memory laps mixed with your emotional happiness reminds me of the movie '50 Dates'. Funny.
John E
Thursday, October 11, 2007 9:46:02 AM (W. Europe Standard Time, UTC+01:00)
LOL - thanks, but I surehope that it is not that bad :-o
Thursday, October 11, 2007 10:54:25 PM (W. Europe Standard Time, UTC+01:00)
Of course, your post's title ("Delphi Language - Did you know") is wrong. What you describe is a feature of the run-time library, not of the Delphi language.
Tuesday, October 16, 2007 12:16:21 PM (W. Europe Standard Time, UTC+01:00)
You probably didn't read Mastering Delphi :) Marco was writing about thees.
Tuesday, October 16, 2007 12:19:38 PM (W. Europe Standard Time, UTC+01:00)
Actually, I have at least three of the series, but I did not read them page by page, and probably did not remember every single tidbit in there either ;)
Comments are closed.
All content © 2008, Daniel Wischnewski
On this page
Archives
Promoted Links
Blogroll OPML
My current Flickr Images
www.flickr.com
Dies ist ein Flickr Modul mit �ffentlichen Fotos und Videos von dwischnewski. Ihr eigenes Modul k�nnen Sie hier erstellen.
Recommendations
Sitemap
Special Pages
Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

Theme design by Jelle Druyts