Board index » delphi » Delphi Newbie needs OOP Help.

Delphi Newbie needs OOP Help.

Assuming this is true...

  ...
  var
    ObjectA, ObjectB: TAnObject;

  begin
    ObjectA:=  TAnObject.Create;
    ObjectB:=  TAnObject.Create;
  ...

     How can you copy the "data members" or "state" of ObjectA
to ObjectB without the address of ObjectB pointing to ObjectA.
Assuming the code above gives ObjectA and ObjectB two different
instances and addresses, I'd like to know how to make a copy from
one to another without having to deal with such members directly.

I've tried to do this...

  ...
  ObjectB:=ObjectA;
  ...

to try to copy only to have two ObjectB begin the same as ObjectA in "data
members" and address.  Does anyone have an easy solution?

 

Re:Delphi Newbie needs OOP Help.


Quote
Salvador S. Cecilio wrote:

> Assuming this is true...

>   ...
>   var
>     ObjectA, ObjectB: TAnObject;

>   begin
>     ObjectA:=  TAnObject.Create;
>     ObjectB:=  TAnObject.Create;
>   ...

>      How can you copy the "data members" or "state" of ObjectA
> to ObjectB without the address of ObjectB pointing to ObjectA.
> Assuming the code above gives ObjectA and ObjectB two different
> instances and addresses, I'd like to know how to make a copy from
> one to another without having to deal with such members directly.

> I've tried to do this...

>   ...
>   ObjectB:=ObjectA;
>   ...

> to try to copy only to have two ObjectB begin the same as ObjectA in "data
> members" and address.  Does anyone have an easy solution?

to copy ObjectA to ObjectB do  
         ObjectB.field := ObjectA.field for every field of TAnObject.
Sincerely, R. Bullock

Re:Delphi Newbie needs OOP Help.


In article <4t9uo6...@rigel.pixi.com>, "Salvador S. Cecilio" <"Salvador "@ pixi.com> wrote:

Quote
>Assuming this is true...

>  ...
>  var
>    ObjectA, ObjectB: TAnObject;

>  begin
>    ObjectA:=  TAnObject.Create;
>    ObjectB:=  TAnObject.Create;
>  ...

>     How can you copy the "data members" or "state" of ObjectA
>to ObjectB without the address of ObjectB pointing to ObjectA.
>Assuming the code above gives ObjectA and ObjectB two different
>instances and addresses, I'd like to know how to make a copy from
>one to another without having to deal with such members directly.

>I've tried to do this...

>  ...
>  ObjectB:=ObjectA;
>  ...

>to try to copy only to have two ObjectB begin the same as ObjectA in "data
>members" and address.  Does anyone have an easy solution?

When you define TAnObject you need to have an Assign method that does
F1 := B.F1 ... Fn = B.Fn

Procedure Assign (B : TanObject) ;

John

Re:Delphi Newbie needs OOP Help.


Assign should do what you want.

ObjectB.Assign(ObjectA);

John.

Re:Delphi Newbie needs OOP Help.


Quote
john atkins wrote:

> Assign should do what you want.

> ObjectB.Assign(ObjectA);

Delphi Help quote:

Assign: " Applies to

TBitmap, TBrush, TClipboard, TControlScrollBar, TFieldDef, TFieldDefs,
TFont, TIcon, TIndexDef, TIndexDefs, TMetafile, TParam, TParams, TPen,
TPicture, TStringList, TStrings objects; TField components"

Thus, it is not defined for general objects. You can make your own
B.Copy(A)  { copy A to B}  by  

  B.field := A.field for every field of the object.

Sincerely, R. Bullock.

Other Threads