Thanks for the responses. I'm kicking myself for not seeing the simple "assign" concept. Just learning to stop thinking procedurally.

 

Cheers

Dave Jollie
Developer, TOWER NZ IT

(: 09 368 4259
Ê: 09 306 6801
*: [EMAIL PROTECTED]
.: 46 Parnell Rd, Parnell, Auckland

-----Original Message-----
From: Phil Middlemiss [mailto:[EMAIL PROTECTED]
Sent: Thursday, 28 August 2003 8:31AM
To: Multiple recipients of list delphi
Subject: Re: [DUG]: How do I copy the state of one object to another?

 

Dave,

 

as strange as it may seem, you are not far off. The pattern that is used widely in the VCL is having an Assign method. For example

 

TAnimal = class(TObject)

    procedure Assign(Source : TObject); virtual;

    property LegCount etc

    property Colour etc

end;

 

TDog = class(TAnimal)

    procedure Assign(Source : TObject); override;

    property BarkDB etc

    property Breed etc

end;

 

....

 

procedure TAnimal.Assign(Source : TObject);

begin

  if Source is TAnimal then

    begin

    LegCount := TAnimal(Source).LegCount;

    Colour := TAnimal(Source).LegCount;

    end

  else

    raise some kind of exception

end;

 

procedure TDog.Assign(Source : TObject);

begin

  inherited Assign(Source);

  if Source is TDog then

    begin

    BarkDB := TDog(Source).BarkDB;

    Breed := TDog(Source).Bark.DB;

    end

  else

    raise some kind of exception

end;

 

 

You could try using a generic RTTI routine but these can get messy, hard to read later, and outdated by changed in the compiler.

 

Phil.

----- Original Message -----

Sent: Wednesday, August 27, 2003 4:50 PM

Subject: [DUG]: How do I copy the state of one object to another?

 

Hi all

 

I want to copy the state (variables) of one instance object of a class, to another instance object of the same class. Eg. Class TDog, instance Dog1 and Dog2. I could do the following:

Dog2.Colour := Dog1.Colour;

Dog2.Breed := Dog1.Breed;    etc.

But this seems a little silly. How do I do this in OO?

 

If knowing more would be helpful, here's what I'm doing:

 

I'm using ICS to connect to an smtp server and want to make multiple connections so that I can offload 4000 emails as quickly as possible, leaving the smtp server to send them out. (No I'm not a spammer, this is a report - email with pdf attachment).

 

I've just run a test and I can paste multiple ICS components on my form i.e. SmtpCli1, SmtpCli2, SmtpCli3, etc. And I can assign my email details into SmtpCli1 as my "holder' object. When I go to send, I see if SmptCli2 is in a ready state - if so, I copy all of the details from SmtpCli1 to SmtpCli2 and send via Cli2. Else, I try SmtpCli3, etc. Using this method, and keeping 5 sessions active, I found it sped up my job 5 fold - so seems worth it.

 

So this all works but it's very manual with lots of code. How do I do this with OO elegance?

 

Cheers

Dave Jollie
Developer, TOWER NZ IT

(: 09 368 4259
J: 09 306 6801
*: [EMAIL PROTECTED]
.: 46 Parnell Rd, Parnell, Auckland

 

Reply via email to