> Let's say I have a class defined as follows
>
> type
> Class X = class (TPersistent)
> 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:
>
> with Source as X do file://Still can't access any properties
This should be working although it returns nil if Source isn't class X or if Source is
nil
and you get an AV...
Casting is risky the most robust seems to be
if Source is X then with X(Source) do begin
end
else // This was the wrong class or was nil
Still doesn't explain why you can't access your properties, you should be able
to using this cast. I'm surprised that the code below won't work.
with Source as X do self.FProp := Prop;
> Source := X(Source); file://Still can't access any properties
Since source is TPersistent and your class is X this line shouldn't even compile.
> 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.
It's true that generally you have no real need to override ASsign from tpersistent
since your usually copying X to X but it's possible in the code here to accept
other classes and then assign them over...
TMyClass.Assign(StringList); can be implemented easily enough with a check
of class... And if you correctly override AssignTo you can also do the
StringList.Assign(TMyClass)... Even if TMyClass is nothing to do with TStrings...
Well here's mine which is indeed an overridden TPersistent
type
Tbase = class(TPersistent)
public
procedure Assign(Source :TPersistent); override;
end;
Tchild = class(TBase)
public
procedure Assign(Source :TPersistent); override;
end;
implementation
procedure Tbase.Assign(Source :TPersistent);
begin
if Source is Tbase then begin
FMyaseProperty := Tbase(Source).MyBaseProperty;
exit;
end;
inherited;
end;
procedure Tchild.Assign(Source :TPersistent);
begin
inherited;
if Source is Tchild then begin
FMyChildProperty := Tchild(Source).MyChildProperty;
end;
end;
Your code must be at fault for this not to work.
--
Aaron Scott-Boddendijk
Jump Productions
(07) 838-3371 Voice
(07) 838-3372 Fax
---------------------------------------------------------------------------
New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
Website: http://www.delphi.org.nz