• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 200
  • Last Modified:

what is wrong with this..(trying to use assign)

i have this class..

type
  test = class(tpersistent)
  private
    Fp: string;
    procedure Setp(const Value: string);
  public
  property p : string read Fp write Setp;
end;

and i want to use like this

procedure TForm1.Button1Click(Sender: TObject);
var
  a, b : test;
begin
  a := test.Create;
  a.p := 'something'
  b := test.Create;

  b.Assign(a);
  showmessage(b.p);
end;

and i recevive un error message cannot assign test to test...what is wrong?..
0
ThoseBug
Asked:
ThoseBug
  • 2
2 Solutions
 
jpedefCommented:
Take a look to TPersistent's source.
You find that you must manually override either Assign or AssignTo procedure.
0
 
ThoseBugAuthor Commented:
sorry...can you be more specific..?..a line of code may be..?
0
 
jpedefCommented:
Example code:

type
  test = class(tpersistent)
  private
    Fp: string;
    procedure Setp(const Value: string);
  protected
    procedure AssignTo(Dest: TPersistent); override;
  public
  property p : string read Fp write Setp;
end;

...
procedure test.AssignTo(Dest: TPersistent);
begin
  if Dest is test then
    (Dest as test).Setp(Fp)
  else
    inherited; //** Raises an EConvertError
end;
0
 
mikelittlewoodCommented:
If you wrote the assign as above, you would still have to write the physical code for each of the properties of the object - which basically defeats the the idea of an assign.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now