Board index » delphi » Copying contents of an object to another object of the same type

Copying contents of an object to another object of the same type

I would like to copy an object to another object of the same type.

Something like Object1 := Object2; doesn't work, because it only lets
Object1 point to the data of Object2. If I would free Object2, Object1
would be freed too.

Anyone knows a solution?

Thanx in advance,
Ritchie

 

Re:Copying contents of an object to another object of the same type


If the object descents from TPersistent (and a lot do), the delphi help
says:

Call Assign to copy the properties or other attributes of one object from
another. The standard form of a call to Assign is

Destination.Assign(Source);

which tells the Destination object to copy the contents of the Source object
to itself.

Most objects override Assign to handle the assignment of properties from
similar objects. When overriding Assign, call the inherited method if the
destination object can't handle the assignment of properties from the class
of the Source parameter.

If no overridden Assign method can handle the assignment of properties from
Source, the method implemented in TPersistent calls the source object's
AssignTo method. This allows the source object to handle the assignment. If
the Source object is nil, TPersistent.Assign raises an EConvertError
exception.

Regards,
  Erwin

Quote
"Ritchie" <osi...@casema.net> wrote in message

news:38a571ef.13423705@news.casema.net...
Quote
> I would like to copy an object to another object of the same type.

> Something like Object1 := Object2; doesn't work, because it only lets
> Object1 point to the data of Object2. If I would free Object2, Object1
> would be freed too.

> Anyone knows a solution?

> Thanx in advance,
> Ritchie

Re:Copying contents of an object to another object of the same type


Bedankt!

On Sat, 12 Feb 2000 17:50:07 +0100, "Erwin Molendijk"

Quote
<E.J.Molend...@student.utwente.nl> wrote:
>If the object descents from TPersistent (and a lot do), the delphi help
>says:

>Call Assign to copy the properties or other attributes of one object from
>another. The standard form of a call to Assign is

>Destination.Assign(Source);

>which tells the Destination object to copy the contents of the Source object
>to itself.

>Most objects override Assign to handle the assignment of properties from
>similar objects. When overriding Assign, call the inherited method if the
>destination object can't handle the assignment of properties from the class
>of the Source parameter.

>If no overridden Assign method can handle the assignment of properties from
>Source, the method implemented in TPersistent calls the source object's
>AssignTo method. This allows the source object to handle the assignment. If
>the Source object is nil, TPersistent.Assign raises an EConvertError
>exception.

>Regards,
>  Erwin

>"Ritchie" <osi...@casema.net> wrote in message
>news:38a571ef.13423705@news.casema.net...
>> I would like to copy an object to another object of the same type.

>> Something like Object1 := Object2; doesn't work, because it only lets
>> Object1 point to the data of Object2. If I would free Object2, Object1
>> would be freed too.

>> Anyone knows a solution?

>> Thanx in advance,
>> Ritchie

Other Threads