51 of 208
ZBasic Language Reference 43 ZX Microcontroller Family 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 addr = freq.DataAddress It is possible, also, to use the DataAddress property to get the address of an element of a persistent array. To accomplish this, simply add parentheses following the property name and specify the index or indices of the item of interest. The example below will result in addr having the Persistent Memory address of the fourth data value of the freq() array. addr = freq(4).DataAddress Persistent structures may also be defined, see Section 3.25 for more details. A Persistent variable may also be defined using Based keyword, see Section 3.21 for more details. For compatibility with BasicX, an alternate syntax is also supported for defining persistent variables as shown below. {Public | Private | Dim} <name> as [New] <persistent-type> Caution: although Persistent Memory data items can be modified, the memory in which they are stored has a write cycle limit of approximately a million writes. Writing to a particular address in excess of this limit may cause the memory to become unreliable. Also, writing to Persistent Memory is much slower than writing to RAM-based variables. The keyword New is optional except in BasicX compatibility mode when it is required. The <persistent-type> element specifies the type of persistent variable being defined and may be one of the following special types: