Type Casting
The instantiation of a language object by a parameterised constructor, or the execution of a language object method, allows for a parameter list.
The parameter list can contain a multiple parameters each of which can be a DataBasic variable or expression but, unlike standard DataBasic calls and external user functions, these parameters are passed by value not reference and so cannot be altered by the method. However, note that when a parameter is an object reference the called method can change the contents of that object.
Valid parameters are characters, strings (including dynamic arrays), integers, floating point numbers, booleans and DBOs. (Note that complete dimensioned arrays or vectors cannot be specified, only specific individual elements.)
Each parameter can optionally be cast at compile time by prefixing a type in parentheses. The type can be specified in uppercase or lowercase, but not mixed case; for example (FLOAT) and (float) are valid, but (Float) is not.
If the type is not specified then at run time the actual parameter is examined and an attempt made to auto-type it. In general auto-typing works well and there is little need for type casting.
Note
Although type casting is most commonly encountered in this context, in fact you can cast any expression that is assigned to a property or field.
Syntax
{(type)} expression
Syntax elements
expressionAny valid DataBasic expression.
typeSignifies that the expression is:
[u|U][int|INT][8|16|32|64]A (possibly unsigned) integer, and whether 8-, 16-, 32- or 64-bit.
[u|U][char|CHAR]A (possibly unsigned) character.
.NET does not support unsigned characters; the DNLS maps 8-bit DataBasic characters to .NET Unicode characters, and vice versa.
[object|OBJECT]A DataBasic object (DBO).
[bool|BOOL] A Boolean (logical true or false)
Generic DataBasic expressions can be cast as Boolean where an empty string or numeric zero will be considered "false", and anything else will be considered "true".
Methods that return a Boolean result return numeric 0 for false, 1 for true, to enable the result to be used directly in a DataBasic expression. If the result is to be passed to another method as a parameter it will require casting.
[float|FLOAT]A floating point number.
[string|STRING]A string (includes string numbers and dynamic arrays).
String numbers can use the full Java floating point syntax; 1.23e5f
. However such numbers cannot be used in DataBasic arithmetic expressions.
Applicability
Language objects only.
Examples
Lowest negative eight bit integer.
(int8) -128
Largest positive sixteen bit integer.
(int16) 32767
Largest negative thirty-two bit integer.
(int32) -2147483648
Largest positive sixty-four bit integer.
(int64) 9223372036854775807
Force a string to be a character.
(char) "z"
Force a numeric zero to be Boolean false.
(bool) 0
Force a non empty string to be Boolean true.
(bool) "verified"
Force an integer to be a float.
(float) "123"
Force a number to be a string.
(string) "123"
A null object.
(object) 0
Uses the Boolean type returned by the endsWith() method directly in a DataBasic expression and prints True.
Hi = %Connect(JAVA)->%New(String, "Hello World")
IF Hi->endsWith("ld") THEN CRT "True" ELSE CRT "False"