Page 55 of 229 Start Back Next End ZBasic Language Reference 45 ZBasic Microcontrollers Using the keyword Dim has the same effect as using Private. Within a subroutine or function, a persistent variable is defined using the syntax. Dim <name> as Persistent <type> In both cases, the <type> element may be any numeric type (e.g. Byte, Integral, Single, etc.), Boolean or a user-defined type (structure or enumeration). A persistent string must be defined using the bounded string syntax (Section 2.11.1), i.e. Dim <name> as Persistent BoundedString(<size-expr>) The <size-expr> element must be a constant integral expression that specifies the number of bytes to reserve for the persistent string’s characters. Examples Dim kbdAttached as Persistent Boolean Private signOnMsg as BoundedString(25) It is important to note that the implementation of the PersistentString type is identical to that of the BoundedString type and is therefore not protected from overwriting the boundaries of the data item. To protect against overwriting, it is advisable to explicitly limit the size of the string to be written. Arrays of persistent variables may be defined as well. To do so, simply add the array dimension list to the variable name in the same manner as for regular variables. If no lower bound is specified, the default array base applies. Example Dim freq(1 to 10) as PersistentInteger It is important to note that persistent variables are not initialized by the system. They have values based on whatever data happens to be at the Persistent Memory address to which they are assigned. The compiler assigns Persistent Memory addresses in the order that modules are compiled and, within modules, in the order the variables are defined. To avoid problems of unexpected address order changes, it is highly recommended that all persistent variables be defined in a single module. Also, it is recommended when you add more persistent variables to an existing application that you add them following the definitions of the previously existing persistent variables. Deleting persistent variables or inserting new persistent variables in the midst of existing ones may cause problems because it will change the address to which subsequent variables are assigned. You’ll probably want to build into your application a way to initialize all of your persistent variables to a known state. This initialization only needs to be done once, when the application is first installed (the FirstTime() function may be useful for this purpose). It may also be useful, however, to be able to do this at other times as well. Another useful technique is to include a persistent variable whose only purpose is to indicate that the persistent variables have been properly initialized. For this to work, you would need to choose a value that is unlikely to otherwise occur. It may even be advisable to place such a “sentinel” variable at both the beginning and the end of the group of persistent variables, decreasing the likelihood of false positive or false negative indications. Persistent variables have an associated property named DataAddress. The value of this property is the address of the data item in Persistent Memory. The type of the property is UnsignedInteger for compatibility with the PersistentPeek() subroutine. Example Dim freq(1 to 10) as PersistentInteger Dim addr as UnsignedInteger