Cast

FreeBASIC

Cast
 
Converts an expression to a specified data type

Syntax

Cast( datatype, expression )

Description

Converts expression into a different datatype. Useful to be used in macros when datatype is unknown and also when converting to Type Alias.

Note: this is a general form of conversion operators such as CInt or CDbl. They are more versatile because they can be used on types that have a Cast Operator, but don't have a built-in keyword for it. e.g. Cast( my_type, expr). They are also suitable for use in cases where the type of a variable is not fixed in the code - for example, it might be Defined earlier, or may be the Type Of a different variable or expression.

Note: If you want to use an operator specifically for converting to different types of Pointers, consider using CPtr instead.

Example

'' will print -128 because the integer literal will be converted to a signed Byte
'' (this Casting operation is equivalent to using CByte)
Print Cast( Byte, &h0080 )

'' will print 3 because the floating-point value will be converted to an Integer
'' (this Casting operator is equivalent to using CInt)
Print Cast( Integer, 3.1 )


Dialect Differences

  • Not available in the -lang qb dialect unless referenced with the alias __Cast.

Differences from QB

  • New to FreeBASIC

See also