![]() ZBasic System Library
91
ZBasic Microcontrollers
Type
Function returning a converted type (see discussion below)
Invocation
CType(value1, enumType) or
CType(value2, typeCast)
Parameter
Method
Type
Description
value1
ByVal
numeric or Enum
The value to convert to another type.
enumType
ByVal
Enum
The name of an Enum type.
value2
ByVal
any type
The value to convert to another type.
typeCast
ByVal
string
A C/C++ typecast.
Discussion
In the first form shown above, this function converts any numeric value or enumeration member to be a
member of specified enumeration type. No checking is done to confirm that the given value actually
corresponds to one of the members of the enumeration. See the section on enumerations in the ZBasic
Reference Manual for more information.
Example
Enum Color
Red
Green
Blue
End Enum
Dim c as Color
c = CType(1, Color)
' c will have the value Green
In the second form, useful only for native mode devices, the specified value is emitted along with the
specified typecast string in a form intended to coerce the value to a desired type. The typecast string,
which may be any valid C/C++ cast, may have one of two forms. If the typecast string contains a dollar
sign, the value given, which may be an arbitrarily complex expression, is substituted in place of the dollar
sign and the result is emitted. If no dollar sign is present in the typecast string, the typecast string is
emitted verbatim followed immediately by the value (enclosed in parentheses if the value comprises a
complex expression).
Examples
Call foo(CType(val + 10, "(char *)"))
addr = CType(bar(3), "reinterpret_cast<uint16_t>($)")
In the first example, assuming that foo() is an external C/C++ function that requires a char * parameter,
the generated code would look something like this:
foo((char *)(zv_val + 10));
In the second example, assuming that bar() is an external C/C++ function that returns a pointer of some
type, the generated code would look something like this:
zv_addr = reinterpret_cast<uint16_t>(bar(3));
|