STWA6-Possible bad typecast (for objects: consider using "as")

Top  Previous  Next

 

Possible bad typecast (for objects: consider using "as") (STWA6)

 

This section lists locations in your code with a possibly bad typecast. If you use the "as" operator, an exception will instead be raised. Otherwise there may be access violations and errors in a totally different code location, which is not very easy to track down.

 

Example:

 

clipx183

 

 

In the example above, the last line could better be written (although still faulty!) as

 

Monkey := Banana as TAnimal;

 

This should result in an exception. But this is still preferable; instead of letting the code proceed resulting maybe in access violations later in a totally unrelated part of the code.

 

Also situations where a "bigger" type is typecast to a "smaller", will trigger a warning. For example "Ch := Char(I)" where Ch is of type Char and I is of type Integer. This may of course be totally valid if you make sure that I is not too big.

 

 

See also:

 

Alerts