53 of 172
ZBasic Language Reference 46 ZX Microcontroller Family The keyword New is optional except in BasicX mode where it is required for compatibility reasons. When defining a fixed-length string the <size-expr> should be a constant integral expression specifying the number of bytes to allocate for the string’s characters. Example Dim msg as String * 15 This definition will create a string variable that always contains exactly 15 characters. 2.12 Variable Initialization All statically allocated variables are initialized by the system immediately prior to Main() beginning execution. For String types, this means that the bytes comprising the variable are set to represent an empty string. For all other types, the constituent bytes are set to zero. Variables defined at the module level and those defined using Static within a subroutine or a function are statically allocated and are, therefore, initialized. Dynamically allocated variables are not initialized by the system except for String types which are initialized to represent an empty string. Variables defined using Dim within a subroutine or a function are dynamically allocated. When you define a variable you may provide an initial value by adding an equal sign and the desired value (which must be a constant vaued expression) following the variable’s type. Initialization is not supported for arrays, structures, Based or Alias variables nor for Program Memory or Persistent Memory data items. Examples Dim count as Integer = 5 Dim str as String = "column" 2.13 Type Conversions The ZBasic language is strongly typed meaning, for example, that it is not allowed to assign the value of a constant, variable or parameter of one type to a variable or parameter of a different type. There are two apparent exceptions to the strong-type regimen. The first exception is with respect to integral numeric literals. An integral literal is considered to have a universal integral type (32-bit internally) so it can be assigned to a parameter or variable of any integral type (Byte, Integer, UnsignedInteger, etc.). Note that the presence of a plus sign or minus sign on a numeric literal does not change this interpretation so it is allowable to assign the value –¹ to an unsigned variable type. The second apparent exception to the strong typing rules occurs with the System Library routines. Many of these routines will accept two or more data types for some of their parameters. It is as though several different versions of the library routines exist, differing only in the types of the parameters that they accept. This computer science concept is known as polymorphism. The System Library routines include a set of functions for performing type conversions. The first set, CBool(),CByte(), CInt(), CUInt(), CLng(), CULng(), CSng() and CStr() allows, with some exceptions, conversion of a value of an arbitrary type to the target type. The second set, FixB(), FixI(), FixUI(), FixL(), and FixUL() are specifically for converting Single values to the target type. The difference between using FixI() and CInt(), for example, to convert a Single value is the rounding method used. The final set of conversion functions, CType() and To<enum>() is for converting an integral value to an enumeration member. See the ZBasic System Library Reference Manual for more details on these conversion functions.