CType

From Xojo Documentation

Revision as of 19:03, 19 November 2009 by WikiSysop (talk) (Examples)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


Description

Performs explicit type conversion.


Syntax

result = CType (value, datatype)

Name Type Description
result Any The passed Value converted to the datatype DataType.
value Any The value to be converted to the passed DataType
datatype DataType One of the REALbasic datatypes.


Notes

The CType operator is the explicit version of type conversion. Implicit conversion is available via the assignment (=) operator.


CType does not necessarily preserve the value across the types. It converts the passed value source type to the destination data type. In that regard, it is identical to implicit conversion. If you are using CType to convert a real number data type (e.g., single, double, currency) to an integer data type, it truncates the value rather than rounds. Sign extension is preserved when converting from a signed data type to another signed data type. Conversion between signed and unsigned types (either way) preserves the bits but not the value.


Examples

The following compares implicit vs. explicit type conversion.

Dim s as Single

i=5
s=i //implicit conversion
s=CType(i,Single) //explicit conversion

The following examples illustrate loss of information in the conversion.

dim d as Double
dim ui as UInt8
d=566.75
i=CType(d,Integer) //returns 566
i=-10
ui=CType(i,UInt8) //returns 246

See Also

= operator.