Struct Statement

Defines a simple compound data type.

Syntax

Struct struct_name [ < type_list > ] [ Is base_type ]
    element_name As element_type
    ...
    [ statements ]
End [ Struct ]

Parts

struct_name
Necessary identifier
type_list
Optional: See Type List for more information.
base_type
Optional value type: either an elementary data type or the name of a different Struct.
element_name
Necessary identifier
element_type
Necessary value type: either an elementary data type or the name of a different Struct.
statements
Optional procedures
End
Completes the Struct statement. Alternatively, you can use End Struct.

Instructions

Struct is short for “structure”.

You can place a Struct only within certain contexts. These contexts include modules (Program and Library), and declarations of Class, Object, and Trait. You cannot place a Struct within a procedure. See Declaration Contexts and Default Access Levels for more information.

Allowed members

Bit fields

A bit field is an integer member whose size in bits is specified in code. You use the SBit and UBit data types only within a Struct. You must supply the size between rounded brackets immediately after SBit or UBit. The size must be an integer literal between 1 and 128.

UBit is short for “unsigned bit field”.

SBit is short for “signed bit field”. When you use SBit, the most significant bit is reserved as a sign bit. Thus, it is not possible to define an “SBit(1)”. In this case, it will be treated as “UBit(1)”.

A base type is most useful when used with bit fields. When you provide a base type, you set the size of the Struct. If the combined sizes of the members is more than the size of the base type, this is an error. If the size of the members is less than the base type, then there are unused bits called padding.

If a Struct mixes bit fields and named types, the named types will usually be placed on boundaries. The type of CPU sets the boundaries, counted from the start of the Struct: Intel CPUs usually have 8-bit boundaries, while Motorola CPUs usually have 16-bit boundaries. A bit field should not go across a boundary.

Examples

Listing 1
Struct point
    x As Real64
    y As Real64
End Struct

See also