Well obviously your method is going to have to have the same signature
(parameters etc) as TPersistent.Assign:

procedure Assign(Source: TPersistent); override;

You're then going to want to see if you can handle the source object that is
being passed in. Remember that it could be _anything_ derived from
TPersistent.

begin
  if (Source <> Nil) and (Source is TMyClass) then

Now you are safe to do a type cast, and as you know what class you've got,
you can use a fast 'unsafe' cast.

    MyField := TMyClass(Source).MyField;

OR perhaps

    with TMyClass(Source) do
    begin
      { Do something }
    end;

How do you handle the case where Source is NOT a type you know how to
handle? Call the inherited method. Maybe the base class knows how to perform
the assignment. If not, it will call Source.AssignTo, to see if Source knows
how to assign itself to your class. Failing that, it will raise an
exception.

  else
    inherited Assign(Source);
end;

If you've got the VCL source, its worth reading the code that handles
assignment (Assign and AssignTo) in units such as Classes.pas (where
TPersistent is defined) and Graphics.pas (lots of examples).

Cheers,
  Andrew Cooke.

> -----Original Message-----
> From: Patrick Dunford [SMTP:[EMAIL PROTECTED]]
> Sent: Tuesday, August 24, 1999 10:42 AM
> To:   Multiple recipients of list delphi
> Subject:      [DUG]:  TPersistent.Assign override
> 
> OK next question
> 
> Let's say I have a class defined as follows
> 
> type Class X = class (TPersistent)
> //attributes
> ...
> //operations
> procedure Assign (Source: TPersistent); override;
> 
> OK I need an Assign procedure so in accordance with what I know about
> TPersistent I have to override the inherited Assign operation.
> 
> procedure X.Assign(Source: TPersistent);
> ...
> 
> The problem comes when I try to access the properties of Source, which the
> compiler says are undeclared. The following have all been tried with
> various
> problems:
> 
> procedure X.Assign(Source: X); //declaration differs from inherited
> 
> with Source as X do //Still can't access any properties
> 
> Source := X(Source); //Still can't access any properties
> 
> 
> As of this moment I went back to using a TObject and declaring an Assign
> procedure which, since there is no inherited Assign to override, works
> just
> fine.
> 
> ============================================
> Patrick Dunford, Christchurch, NZ
> http://patrick.dunford.com/
> 
> 
> 
> 
> 
> ============================================
> Patrick Dunford, Christchurch, NZ
> http://patrick.dunford.com/
> 
> --------------------------------------------------------------------------
> -
>     New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
>                   Website: http://www.delphi.org.nz
---------------------------------------------------------------------------
    New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
                  Website: http://www.delphi.org.nz

Reply via email to