Start Back Next End
  
ZBasic Language Reference
90
ZBasic Microcontrollers
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
End Sub
End Namespace
Sub Main()
Debug.Print foo::a
Call foo::SetVal(10)
Debug.Print foo::a
End Sub
Outside the namespace, references to entities defined in the namespace must be preceded by the
namespace prefix (the namespace name and a double colon) as seen in the Main() subroutine. 
However, within the namespace itself the namespace prefix may be omitted.
It is important to note that the foo namespace could also have been written as shown below.
Namespace foo
Dim a as Integer
End Namespace
Namespace foo
Sub SetVal(ByVal v as Integer)
a = v
End Sub
End Namespace
Multiple occurrences of the same namespace are effectively aggregated into a single namespace
definition.  Note, however, that the compiler will issue an error if the visibility of the namespace (i.e. Public
vs. Private) is not the same in all cases.
Although rarely necessary, it is permissible to place a namespace definition within a namespace
definition, such nesting allowed to an arbitrary depth.  In such a case, namespace prefixes must be added
to identifiers beginning at the outer level and moving inward until the identifier itself is reached.  For
example, foo::bar::myVar would be used (external to namespace foo) to refer to the variable myVar
defined within the namespace bar itself defined within the namespace foo.
Previous page Top Next page