ZBasic Language Reference
92
ZBasic Microcontrollers
[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,
etc. may be defined without needing to worry about duplication of names defined elsewhere. That is the
purpose of a ZBasic namespace.
By default, namespace support is not enabled. The compiler directive Option Namespaces or the
command line option -namespaces must be used to enable namespace support (note, however, that
namespace support is enabled automatically when ZBasic object support is enabled). A namespace is
defined using the syntax below, essentially being a wrapper around the definitions of the entities defined
within it.
[Public | Private] Namespace <name>
<definitions of variables, procedures, etc.>
End Namespace
If neither Public nor Private is specified, the namespace is private by default. It is important to note
that all entities defined within a private namespace are forced to be private as well; warnings are emitted
for entities defined with a Public modifier inside a private namespace. In contrast, private entities within
a public namespace are allowed.
In order to refer to entities defined within a namespace, it is necessary to specify both the namespace
and the entity name. Consider the example below.
Namespace foo
Dim a as Integer
Sub SetVal(ByVal v as Integer)
a = v
|