Board index » cppbuilder » copying vcl objects

copying vcl objects

hi,
  how can make a copy of an vcl object ?....

for example....

  TObject *One;
  TObject *Two;

  Two = One;

 how can i copy the properties of One to Two and if i modify One and Two
must not be affected.

chris & harold of team b, said that i must use the assign() method of that
object if any....

do u any more suggestions.....thanks.

thanks a lot guys,
Orly

 

Re:copying vcl objects


Hi, Orly!

There is one other way, which involves the use of the streaming system.
You can create the new object, stream the old object to a TMemoryStream,
reset the TMemoryStream position to the beginning, and stream the
component in from the TMemoryStream using the clone instance as the
Instance parameter. The TStream methods WriteComponentRes and
ReadComponentRes are appropriate. The following is my (untested) take on
this:

    TMemoryStream *CloneStream = new TMemoryStream;
    CloneStream.WriteComponentRes(OriginalComponent);
    SomeComponentClass *CloneComponent = new SomeComponentClass(Owner);
    CloneStream.Position:= 0;
    CloneStream.ReadComponentRes(CloneComponent);
    delete CloneStream;

or even

    TMemoryStream  *CloneStream = new TMemoryStream;
    CloneStream.WriteComponentRes(OriginalComponent);
    CloneStream.Position:= 0;
    SomeComponentClass *CloneComponent =
CloneStream.ReadComponentRes(NULL); // Creates an instance
    delete CloneStream;

I hope this works for you. Note that you should use exception handlers
to ensure everything gets freed - something like what follows:

    TMemoryStream *CloneStream = new TMemoryStream;

    try
    {
        CloneStream.WriteComponentRes(OriginalComponent);

        SomeComponentClass *CloneComponent = new
SomeComponentClass(Owner);

        try
        {
            CloneStream.Position:= 0;
            CloneStream.ReadComponentRes(CloneComponent);
        }
        catch (Exception &VCLException)
        {
            delete CloneComponent;
            throw VCLException;
        }
        catch (...)
        {
            delete CloneComponent;
            throw Exception("Unexpected exception cloning a
component.");
        };
    }
    __finally
    {
        delete CloneStream;
    };

------
Mark Cashman, TeamB C++ Builder
http://www.temporaldoorway.com/programming/index.htm
C++ Builder, JBuilder programming information
Home of The C++ Builder Programmer's Webring - Join us!
------

Re:copying vcl objects


At design time, just cut and paste.
Quote
Orly Reyes wrote:
> hi,
>   how can make a copy of an vcl object ?....

> for example....

>   TObject *One;
>   TObject *Two;

>   Two = One;

>  how can i copy the properties of One to Two and if i modify One and Two
> must not be affected.

> chris & harold of team b, said that i must use the assign() method of that
> object if any....

> do u any more suggestions.....thanks.

> thanks a lot guys,
> Orly

Other Threads