Start Back Next End
  
ZBasic Language Reference
89
ZBasic Microcontrollers
or
{Public | Private | Dim} <name> As ProgMem <type>
where <type> is the name of a previously defined structure.  The allowable members of a Persistent
Memory or Program Memory structure are the intrinsic types Byte, Intege®, UnsignedInteger,
Long, UnsignedLong, Single, bounded strings, arrays of those types and other structures containing
only those types.  Arrays of structures in Persistent Memory or Program Memory may be defined by
specifying the dimensions in the usual way.
It is permissible to directly assign between any combination RAM-based, Persistent Memory and Program
Memory variables defined using the same structure.  Similarly, direct comparison between like structures
(equality and inequality only) is supported.
3.26 Unions
It is occasionally useful to define a data type that is a collection of data elements superimposed on one
another, i.e. where all members occupy the same space.  This yields an effect similar to defining a
variable to be an alias for another.  In ZBasic, as in many other programming languages, such a
collection of data items is referred to as a “union”.  A union is a user-defined data type similar in some
respects to a structure.  As with an structure, you define a union by specifying the consituent elements. 
The syntax for defining a union at the module level is:
[Public | Private] Union <name>
<member-definition>
...
End Union
If neither Private nor Public is specified, the union definition is public.  The ellipsis (…) in the syntax
above connotes that there may be zero or more additional member definitions.  A union definition must
have least one member and may have a practically unlimited number of members.
A union may be defined within a subroutine or function, either at the outer level or within any inner block. 
In this case, the Public and Private keywords have no useful purpose and are therefore not allowed.
Examples
Union FloatBytes
Public byteData(1 to 4) as Byte
Public floatData as Single
End Union
Union MyData
Public b as Byte
Public u as UnsignedInteger
End Union
A variable defined as a FloatBytes type would occupy four bytes of memory since the largest member
is four bytes.  Similarly, a variable defined as a MyData type would occupy two bytes of memory.
3.27 Using Namespaces
In computer science, a namespace is a context within which all identifiers must be unique.  Earlier in this
document it was mentioned that a variable with a particular name, e.g. myVar, may be defined at the
module level and also defined inside a procedure.  This is possible because the module level represents
one context and each procedure represents a separate context.  Sometimes it is useful to define one or
more additional contexts at the module level in which entities such as constants, variables, procedures,
Previous page Top Next page