QMBasic - Type conversion

QMBasic  -  Type Conversion

Top  Previous  Next

 

QMBasic variables are of variant type, the stored type being determined by the context in which the value was set and conversion being carried out on a temporary basis wherever necessary to perform processing. For example, the following program fragment

 

A = 962

S = A[2,1]

 

would result in A containing the integer value 962 and S containing the digit 6 as a string. The type conversion from integer to string is implicit in the use of the substring extraction on the second line.

 

Where a variable is accessed a very large number of times, there may be performance benefits to be obtained from ensuring that it is stored in an appropriate type thus minimising implicit temporary conversions. The QMBasic language does not have any specific type conversion functions as the automatic type variant nature of the language is adequate for most purposes. Where a variable is to be forced to be a number, this can be achieved by adding zero

A = A + 0

or, more typically, combined with some other operation such as

NUM.INVOICES = CLI.REC<C.INV.CT> + 0

 

Similarly, data can be forced to string format by appending a null string

A = A : ""

This is a much less common operation in real programs.

 

QMBasic provides a better way to do type conversion by use of the INT(), NUMERIC(), STR() and BOOL() functions.

 

Developers should avoid over-use of explicit type conversions. The implicit conversion of type variant variables is one of the strong points of the QMBasic language, removing the need for nearly all of the explicit conversions needed in other programming languages.

 

When the national language support decimal separator character is set to something other than the period, both the selected character and the period are recognised as being a valid decimal separator when converting a numeric value in a string to its internal numeric form..

 

The Boolean values True and False can be used interchangeably with numeric values 1 and 0. Any operation that is defined to return a Boolean value in numeric form will return 1 or 0. Any operation that uses a Boolean value will treat 0 and a null string as False, everything else as True.