We help IT Professionals succeed at work.

How to save a component to a file

Device_Diver
Device_Diver asked
on
Hello Experts.

My problem is as follows:
I have several objects, each derived form TGUIObject, which by itself derives from TImage.

What I want to be able to do is save such an object to a file and then load it back, calling the _right_ constructor.

For example, when I load the file I will know whether the object stored inside is TGUIObjectsEditor, TGUIObjectsContainer, etc... This without using:

if ClassName = 'FDGDF' then FDGDF.Create,...
if ClassName = 'AAAAA' then AAAAA.Create...

or any array containing these objects.

Thanks in advance for any help, Amit.
Comment
Watch Question

Commented:
whenever you make a new class, at the bottom of each unit do this

initialization
  RegisterClasses([TMyClass1, TMyClass2, etc]);
end.

When you want to write a component do this
var
  FS: TFileStream;
  Writer: TWriter;
  Size: Integer;
begin
  FS := TFileStream.Create('c:\blah.txt', fmCreate);
  Writer := TWriter.Create(FS, 4096);
  try
    Size := Length(MyComponent.Name);
    FS.Write(Size, SizeOf(Integer));
    FS.Write(MyComponent.Name[1], Size);
    Writer.WriteComponentRoot(MyComponent);
  finally
  end;
end;

To read do this
var
  FS: TFileStream;
  Reader: TReader;
  Size: Integer;
  CompClassName: string;
  CompClass: TClass;
begin
  FS := TFileStream.Create('c:\blah.txt', fmOpenRead);
  Reader := TReader.Create(FS, 4096);
  try
    FS.Read(Size, SizeOf(Integer));
    SetLength(CompClassName, Size);
    FS.Read(CompClassName[1], Size);
    CompClass := FindClass(CompClassName);
    Assert(CompClass <> nil, CompClassName + ' not found');
    MyComponent := CompClass.Create(Self);
    Writer.ReadComponentRoot(MyComponent);
  finally
    Reader.Free;
    FS.Free;
  end;
end;

I have typed this into my browser, so I am not 100% it will compile, but it is very close to being accurate !

Pete
====
http://www.Stuckindoors.com/delphi (Open source)
http://www.HowToDoThings.com (delphi articles)

Commented:
HI,
You can also use the methods ReadComponent and WriteComponent of TStream or TReader (TWriter).
Don't forget to register the classes you read or write with this methods as it was said by Cubud.
Sincerely,
Nestorua.

Commented:
i also need to write a component i have created my components at runtime in a tstringlists and need tosave them and all there vaues (tmemorystream)to a text file for loading please help me got know points
Device_Diver:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
CERTIFIED EXPERT

Commented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

accept cubud's comment as answer

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Thanks,

geobul
EE Cleanup Volunteer

Explore More ContentExplore courses, solutions, and other research materials related to this topic.