DataBasic Reference > DataBasic Objects > DataBasic Server Objects Operations > Variable Typing and Casting

Comment on this topic

Documentation Comments

Use this form to comment on this topic. You can also provide any general observations about the Online Documentation, or request that additional information be added in a future release.

RealityV15.1Online Documentation (MoTW) Revision 7

Variable Typing and Casting (dbo_variable_typing_casting.htm)

To

Reality

Version

Topic

Submitted by

Company

Location

Email address

Comment

 

 

Variable Typing and Casting

All non object variables passed to the language server as method parameters or field assignments are passed as strings. However Java requires all variables to have a specific type. If there is no explicit type cast in the DataBasic code then an attempt will be made to auto type it. In general auto typing works well and there is little need for type casting.

Part of the auto type process involves the string being processed to see if it looks like a number.

Integers

If the string consists entirely of digits (0-9), it is considered likely to be an integer.

Floating point

If the string consists of digits and a single decimal point it is considered likely to be a float.

String numbers can use the full Java floating point syntax - for example, 1.23e5f. However such numbers cannot be used in DataBasic arithmetic expressions.

Boolean

Auto typing to Boolean will only occur if the string contains the lower case values "true" or "false", as these are the Java representations of boolean.

Generic DataBasic expressions can be cast to bool where an empty string or numeric zero will be considered "false", anything else will be considered "true".

Methods that return a boolean result will 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 a (bool) cast.

Casting

An explicit cast can be used to force a string to be converted to a specific type.

Syntax

(type) expression

Syntax Elements

expression Any valid DataBasic expression.
type =
[ [   [int|INT] [8|16|32|64]
    | [char|CHAR]
  ]
  | [bool|BOOL]
  | [float|FLOAT]
  | [double|DOUBLE]
  | [unicode|UNICODE]
  | [string|STRING]
  | [object|OBJECT]
]

Where:

int or INT Signifies that the expression is an integer.
8 or 16 or 32 or 64 Specifies the bit size of the integer.
char or CHAR Signifies that the expression is a single 8 bit character.
bool or BOOL Signifies that the expression is a logical true or false
float or FLOAT Signifies that the expression is a floating point number.
double or DOUBLE Signifies that the expression is a floating point number with increased precision.
unicode or UNICODE Signifies a string of 16-bit characters (in big-endian format) to be processed as Unicode.
string or STRING Signifies that the expression is a string, includes string numbers and dynamic arrays.
object or OBJECT Signifies that the expression is an object.

                        

Examples

(int8) -128

Largest negative eight bit integer.

(int16) 32767

Largest positive sixteen bit integer.

(int32) -2147483648

Largest negative thirty-two bit integer.

(int64) 9223372036854775807

Largest positive sixty-four bit integer.

(char) "z"

Force a string to be a character.

(bool) 0

Force a numeric zero to be Boolean false.

(bool) "my string"

Force a non empty string to be Boolean true.

(float) "123"

Force an integer to be a float.

(string) "123"

Force a number to be a string.

(object) 0

A null object.

MyStr = %New(String, "Hello world")
IF MyStr->endsWith("ld") THEN CRT "True" ELSE CRT "False"

Uses the Boolean type returned by the endsWith method directly in a DataBasic expression and prints True.

 

RealityV15.1 (MoTW) Revision 7Comment on this topic