At 08:45 AM 2/26/2007, CubicDesign wrote:
>(TObjectList[i] as TSomething)
>and
>TSomething(TObjectList[i])
>
>Both codes access the same object; one is using the 'as' operator the
>second one is using a direct typecast. But what are the differences?

The first one checks to see if TObjectList[1] is a TSomething (either 
directly or through inheritance) and then treats the resulting reference as 
a TSomething. If TObjectList[1] is NOT a TSomething a specific exception is 
thrown that you can handle.

TSomething(TObjectList[1]) forces the reference to be treated as a 
TSomething without checking first. In this case any number of exceptions 
can result if TObjectList[1] is not actually a TSomething.

Some would argue that the second form is faster because it omits the check. 
Others would argue that the second form is much more dangerous because of 
the hidden assumption. The second form is much closer to the historic C 
coding style which resulted in so many hard to find bugs or many years. 
Most developers have moved past that style in favor of the more 
conservative usage in the first example.


_______________________________________________
Delphi mailing list -> [email protected]
http://www.elists.org/mailman/listinfo/delphi

Reply via email to