Arrays and Structs

Arrays contain elements and structs contain fields. The elements of an array can be a struct type and a struct can contain one or more fields which are arrays. Therefore, it is possible to have an Array1 of type Struct1, which in turn has as a field, Array2.

Array of a Struct

As an example of an array of a struct, say you define a struct named Student, containing two fields, Name and Id:

Struct: Student

Name: String

Id: Long

Then you can define std as struct Student:

std: Student

One record might be:

std={"John",1}

An array of that struct might be:

astd: Student[2]

astd={ {"John",1},{"Mary",2} }

Array of Struct of an Array

A Struct can also contain a field which is itself an array. Using the previous example, let's expand the original struct Student by making Name into an array of two strings:

aName: Name[2]

where aName(1) is the first name and aName(2) is the last name. Now:

Struct: Student

aName: String

Id: Long

Then you can define std as struct Student:

std: Student

One record might be:

std={{"John","Smith"},1}

An array of that struct might be:

astd: Student[2]

astd={ { {"John","Smith"}, 1 } , { {"Mary","Jones"}, 2 } }

Since arrays are zero-based, the last name of the first student in this example would be:

astd[0].aName[1]

Struct of as Struct

The fields of a struct can also have a struct type as a field. In the following example, the struct Class contains another struct Person:

Struct: Person

aName: String

Id: Long

Struct: Class

CourseName: String

Department: String

CourseNum: Short

Instructor: Person

nStudents: Short

aStudent: Person[40]

We define a history class:

clsHistory: Class

The name of the last student in the history class would be:

clsHistory.aStudent[clsHistory.nStudents-1].aName

See Also

Assignment of Arrays, Strings as Arrays, Struct, Assigning Structs to Structs or to Variants