Navigation bar
  Start Previous page
 87 of 158 
Next page End  

81
Each variable that is so defined will occupy an integral number of bytes of memory.  For the example
above, three bytes will be allocated even though only 20 of the 24 available bits will actually be used.
Note that the ByteAlign attribute may be used with any variable type but it has no effect for those types
that are inherently byte-aligned.  Also, the ByteAlign attribute may be used in a Structure to force sub-
byte types to be byte-aligned.
3.25 Structures
It is often useful to define a data type that is a collection of data elements.  For example, if you write a
program to manipulate dates it may be useful to define a data type that contains “year”, “month” and “day”
elements.  This is convenient because it allows you to think about or refer to the group of data elements
as a single entity rather than as the individual constituent elements.
In ZBasic, as in many other programming languages, such a collection of data items is referred to as a
“structure”.  A structure is a user-defined data type similar in some respects to an enumeration.  As with
an enumeration, you define a structure by specifying the consituent elements.  The syntax for defining a
structure at the module level is:
[Public | Private] Structure <name>
<member-definition>
...
End Structure
If neither Private nor Public is specified, the structure definition is public.  The ellipsis (…) in the
syntax above connotes that there may be zero or more additional member definitions.  A structure
definition must have least one member and may have a practically unlimited number of members.
A structure 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.
After a structure has been defined, the structure name may be used as a <type> in a variable or
structure definition.  A structure may also be used as the <type> in the formal parameter list of a
subroutine or function definition.  Note, however, that a Public subroutine or function cannot be defined
with a parameter that is a Private Structure.
A <member-definition> has the same syntax as that used to define a variable.  As with an ordinary
variable, a member may be a single data element or it may be an array.  The syntax for a member
definition is given by the two descriptions below – the first being for a non-array member and the second
being for a member that is an array.
{Public | Private | Dim} <name> As <type>
or
{Public | Private | Dim} <name>(<dim-spec-list>) As <type>
As with ordinary variables, Dim has exactly the same effect as Private, i.e., the member will only be
directly accessible to code within the module.  In contrast, a Public member may be accessed by code
outside of the module in which the structure is defined.  The names of the members of each structure
defined may be any legal identifier however a particular name may be used only once in each structure.
The use of a name as a member in one structure does not preclude it from also being used as a member
name in a different structure or as a variable, constant, parameter, etc.  This circumstance is a result of
the ZBasic scoping rules - a structure definition is an independent name scope.
The <type> specified for a member may be any of the pre-defined types like Integer, Byte, String,
etc. or it may be a user-defined type like an enumeration or another structure.  The amount of space
required for each member in a structure is the same as for a variable of the same type (but see the
Previous page Top Next page