TypeCasting: what's the difference between the two lines of code?

what's the difference between the two lines of code. Both are trying to get the path and one works and the other is throwing an error. i am working on delphi-7

Path:= (((FFormOwner as TForm).Designer) as IDesigner).GetPrivateDirectory; --Working
Path:= IDesigner(TForm(FFormOwner).Designer).GetPrivateDirectory ;  --Error

      

Below is the code that uses a line of code to get the path.

constructor TsampleComponent.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FFormOwner:=TForm(Owner);
  if not (Owner is TForm) then
    repeat
      FFormOwner:=TForm(FFormOwner.Owner);
    until (FFormOwner is TForm) or (FFormOwner.Owner=nil);

  if (csDesigning in ComponentState) then
    Path:= (((FFormOwner as TForm).Designer) as IDesigner).GetPrivateDirectory
  else
    Path:=ExtractFilePath(Application.EXEName);
.
.

end;

      

+3


source to share


1 answer


IDesigner(TForm(FFormOwner).Designer)

      

Performs simple reinterpretive translation Designer

. It will fail because it Designer

is of a type IDesignerHook

that is not the same as IDesigner

.



(FFormOwner as TForm).Designer) as IDesigner

      

Executes a runtime request for IDesigner

and resolves with a call QueryInterface

. This is the correct way to get a different interface from an existing one.

+8


source







All Articles