tags:

views:

492

answers:

1

Can anyone tell me why the second cast fails to compile in Delphi 7?

var
  WebBrowser: TWebBrowser;
begin
  WebBrowser := TWebBrowser.Create(Self);
  TWinControl(WebBrowser).Parent := Self;
  (WebBrowser as TWinControl).Parent := Self; // fail here
end

Parent in TWebBrowser is a read-only IDispatch property, but why does the first cast see the TWinControl parent property ok but the second one does not?

Thanks

+6  A: 

The first cast use no checking, it assumes the programmer is right and goes on. The second cast uses some sanity checking. (Causes an exception if the cast is invalid). I think in this case, the compiler got confused because of the like named properties. It could even be an overenthousiastic optimizer.

At least,

var
  wc : TWinControl;
begin
  wc := (WebBrowser as TWinControl);
  wc.Parent := Self;
end;

Works. So there is a circumvention.

Gamecat