Thanks,
Tony
Get the source of TSimpleGraph and see how it's done there.
http://delphiarea.com/products/simplegraph/
GC
A general "copy constructor" doesn't exist, but some components support
an Assign method.
DoDi
Some info from my experience is available here
http://blog.dragonsoft.us/2008/04/21/how-to-serialize-delphi-object/
"Tony Caduto" <tony....@gmail.com> wrote in message
news:480bdd94$1...@newsgroups.borland.com...
There is no standard "easy way" in Delphi to clone an existing object.
The Delphi developers choose, in some cases, to provide an Assign method.
Some professional developers use a technique to save
an object to a stream and restore it later.
This is called serialization,
and is describe at:
I generally add a cloning method to those classes who's objects I'll want to
copy during runtime. Something similar to a copy constructor. For example:
var
o1: TMyObject
o2: TMyObject
begin
o1 := TMyObject.Create;
o2 := o1.Clone();
or
o1 := TMyObject.Create;
o2 := TMyObject.Create;
o1.CopyTo( o2);
And then manually implement the method
function TMyObject.Clone(): TMyObject;
begin
result := TMyObject.Create;
result.field1 := field1;
result.field2 := field2;
//etc
end;
XMemoryStream:=TMemoryStream.Create;
try
XTempName:=AAncestor.Name;
AAncestor.Name:='clone_' + XTempName;
XMemoryStream.WriteComponent(AAncestor);
AAncestor.Name:=XTempName;
XMemoryStream.Position:=0;
Result:=TComponentClass(AAncestor.ClassType).Create(AAncestor.Owner);
if AAncestor is TControl
then TControl(Result).Parent:=TControl(AAncestor).Parent;
XMemoryStream.ReadComponent(Result);
finally
XMemoryStream.Free;
end;
end;