Casting a TWebBrowser as a TWinControl
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?
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.
var wc : TWinControl; begin wc := (WebBrowser as TWinControl); wc.Parent := Self; end;
Works. So there is a circumvention.