Skip to content

Struct Declaration

A struct type declaration defines a particular set of elements, called struct members, and associates it with a new type identifier. Once a struct type is declared, you may declare and use variables of that type.

You declare a struct type using the Struct and End_Struct keywords. For example:

Struct tUSAddress
    String sStreet
    String sCity
    String sState
    Integer iZipCode
End_Struct

Struct tPurchaseOrder
    Integer iOrderNumber
    Date dOrderDate
    tUSAddress shippingAddress
    tUSAddress billingAddress
    ...
End_Struct

In the above example, the tUSAddress struct contains four members: three strings named sStreet, sCity, and sState, and an integer named iZipCode.

Struct type names must be unique throughout the program. Struct member names are local to the struct type and need only be unique within the type itself. The DataFlex naming convention is to prefix struct type names with a lowercase "t".

Struct members may be of any valid data type, including other struct types and array types.

Struct tMyStruct
    String sName
    Integer[] iValues
End_Struct

Struct tMyOtherStruct
    Integer iCode
    String sValue
    tMyStruct nestedStructMember
End_Struct

Structs may be nested by specifying struct type members as long as the member struct type has been declared before this struct declaration. Struct members of the declaring struct (i.e., recursive struct declarations) are illegal. However, dynamic arrays of the declaring struct are allowed. This is referred to as indirect-recursive struct declarations.

Struct tTreeNode
    ...
    tTreeNode[] children
End_Struct

Struct members of array type (static, dynamic, and jagged array type members) are allowed. The size of each dimension in static array type members must be a constant computed at compile time.

Structs and JSON/XML

If you are serializing and deserializing structs to and from JSON and XML, you may need to use different internal and external names for struct members for use with web services. For example, this is necessary if a struct member uses a DataFlex reserved word.

The Name Meta-Data Tag is designed for this purpose.

See Also