Skip to content

Cast

See Also: Type Conversion Functions, Data Types, Functions and Constants

Purpose

The Cast function returns a variable of the specified type that is the best conversion from the original variable.

Return Type

Specified type

Syntax

Cast( {Var}, {Type} )

Where:

  • {Var} can be any variable, but not an expression or a constant value.
  • {Type} is the name of a data type, for example, Number.

Example

Procedure TestCast
    UInteger u
    Real r
    Move 3.1415 to r
    // Move Real r to Unsigned Integer u, decimals will drop off
    Move (Convert(r, UInteger)) to u
    Send Info_Box u "Result"
End_Procedure

Example

Cast differs from Convert in that it will not report conversion errors in data overflow situations. Cast will, however, still report an error if a string contains a value that cannot be properly converted to another type. For example:

Procedure TestCast
    String s
    Integer i
    Move ",.1" to s
    Move (Cast(s, Integer)) to i
End_Procedure

Notes

  • The difference between Convert and Cast applies primarily to data-overflow situations, such as changing an integer with a large value to a short. When a number doesn't fit, you get an error with Convert and not with Cast.
  • Strings are special. When you cast or convert from a string, they do the same thing, which is to not raise data-range errors. However, if the format of your string is invalid, you get an error with Cast or Convert.