Tom Blocker wrote:
> A better way...
> type
> TMyForm = class(TForm)
> public
> Test: integer;
> end;
>
> procedure Foo(F: TForm)
> begin
> if (F is TMyForm) then
> (F as TMyForm).Test := 0;
> end;
That's equivalent to this:
if F is TMyForm then
if F is TMyForm then
TMyForm(F).Test := 0
else
raise EConvertError.Create(...);
You never need to use "is" and "as" as the same time. The "as" operator
performs an "is" test anyway.
> Adding an else stmt Assert(False) can do a future programmer a big favor
> too.
You could also just do this:
Assert(F is TMyForm);
Then you could continue with the unchecked cast.
But, if the function is _always_ supposed to take a TMyForm, then it
should be declared to take a TMyForm. Then the compiler will complain when
you try to pass anything else. You get the error fixed at compile time
instead of having to wait until run time. Another benefit is that there is
no type-casting involved anymore, so there is no longer any argument about
which kind of type-cast to use.
--
Rob
_______________________________________________
Delphi mailing list -> [email protected]
http://www.elists.org/mailman/listinfo/delphi