ZBasic Language Reference
89
ZBasic Microcontrollers
{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
discussion below relating to sub-byte types and alignment). It is important to note that recursive structure
definitions, with members that are or contain (directly or indirectly) the structure being defined, are not
allowed.
Examples
Structure MyDate
Public year as UnsignedInteger
Public month as Byte
Public day as Byte
End Structure
Structure MyTime
Public hour as Byte
Public minute as Byte
Public seconds as Single
End Structure
Structure MyTimeStamp
Public tdate as MyDate
Public ttime as MyTime
Private isCurrent as Boolean
End Structure
Sub Main()
Dim ts as MyTimeStamp
Call GetTimeStamp(ts.tdate.year, ts.tdate.month, ts.tdate.day, _
ts.ttime.hour, ts.ttime.minute, ts.ttime.seconds)
End Sub
The example above illustrates how members of an instance of a structure are referenced. The variable
ts is an instance of the MyTimeStamp structure. A member is referenced by appending the member
name to the variable name, separating them with a period. For members that are structures, members of
the subordinate structure are referred to similarly. No spaces are allowed in this construction. In cases
where a variable or a member is an array, the index list directly follows the variable/member name,
preceding the period.
Structure foo
Dim b as Byte
Dim ai(1 to 10) as Integer
Dim ts(1 to 4) as MyTimeStamp
End Structure
|