56 of 208
ZBasic Language Reference 48 ZX Microcontroller Family When defining a bounded string, you replace the <size-expr> with a constant integral expression specifying the number of bytes to allocate for the string’s characters. Examples Const slen as Integer = 7 Dim msg as BoundedString(15) Dim msg as BoundedString(slen + 2) The first definition will create a string variable that can hold up to 15 characters; the second will hold 9 characters. For compatibility with BasicX, the alternate syntax shown below is also supported. New applications should use the definition syntax given above since it allows the use of an expression to specify the length. {Public | Private | Dim} <name> as New BoundedString_<length> 2.11.2 Fixed-Length Strings For compatibility with BasicX, ZBasic supports fixed-length strings. These are similar to bounded strings but with two important distinctions. Firstly, the string size is constant and equal to the specified fixed size. If a string value is assigned that has fewer characters than a fixed-length string variable’s specified size, the remaining characters will be filled with spaces. Secondly, if a string value is assigned having more characters than a fixed-length string’s specified size, the excess characters will be discarded. A fixed-length string is defined using the syntax: {Public | Private | Dim} <name> as [New] String * <size-expr> 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