User Tools

Site Tools


k7:k7.2:k7.2.1:start

7.2.1 Structures - Struct

The following lines describe the syntax for declaring a structure:

PUBLIC STRUCT Identifier
  Field_1 [ Embedded array declaration ] AS [ Datatype ]
  Field_2 [ Embedded array declaration ] AS [ Datatype ]
  ...
  Field_n [ Embedded array declaration ] AS [ Datatype ]
END STRUCT

7.2.1.1.1 Declaration Struct

This declaration defines the structure of students with 5 fields:

Public Struct Student
  Grade As Integer
  BirthDate As Date
  Surname As String
  DG1Course As String
  DG2Course As String
End Struct

Note that when declaring a structure as an access right, you can only set Public. The fields from Year to DG2Course are variables with the Public access right.

7.2.1.2 Arrays of structures

The special feature of the following declaration of a structure course is that in the structure student the field course list[8] is an array with elements of type Struct from the course declaration:

Public Struct Course
  CourseSubject As String
  CourseTeacher As String
End Struct
 
Public Struct Student
  Grade As Integer
  BirthDate As Date
  Surname As String
  CourseList[8] As Struct Course
End Struct

Arrays with elements of the type Struct are not real arrays, because they have very few methods of the original array class:

  • The elements can be accessed via an index.
  • It can be iterated over all elements (list). Information about the length of the array and the array dimensions can be queried.

You can then declare variables of the user-defined (pseudo-)type Struct. First a variable course participant of type Struct and then an array course list[22], in which the relevant data of 22 students are stored and whose elements are of type Struct:

Public CourseParticipant As Struct Student
Public Student As New Student ' Alternatively, this also works ...
Public CourseList[22] As Struct Student
Public CourseList[22] As New Student ' → Error message

7.2.1.3 Accessing Structures and their Fields

After you have declared structures, you will learn how to access the structure declared in the previous section and its fields read and write using variables of type Struct. The following program interface is used to enter the values of the individual fields and their display:

Oberfläche
Figure 7.2.1.3.1: Program interface

First, access to the input mask is released. Then all 5 fields of the variable course participants of type Struct (student) are assigned values or the default values are overwritten and the current content of the variable course participants is inserted into the array course list[22].

Public Sub SaveNewRecord()
  CourseParticipant.Grade = cmbJGS.Text
  Student.LastName = txbLastName.Text
  Student.BirthDate = dbGebDate.Value
  Student.DG1Course = cmbDGF1.Text
  CourseParticipant.DG2Course = cmbDGF2.Text
' Store new record in the array CourseList[].
  CourseList[iRecordNumber] = CourseParticipant
End ' SaveNewRecord()

Alternatively, you can use the With statement to write simplified assignments. The source code is easier to read:

Public Sub SaveNewRecord()
 
  With CourseParticipant
      .grade = cmbJGS.Text
      .surname = txbSurname.Text
      .BirthDate = dbBirthDate.Value
      .DG1Course = cmbDGF1.Text
      .DG2Course = cmbDGF2.Text
  End With
  CourseList[iRecordNumber] = CourseParticipant
End ' SaveNewRecord()

A compact alternative is recommended if you immediately insert the contents of a record in the price list array[22]:

Public Sub SaveNewRecord()
  CourseList[iRecordNumber].Grade = cmbJGS.Text
  CourseList[iRecordNumber].LastName = txbLastName.Text
  CourseList[iRecordNumber].BirthDate = dbBirthDate.Value
  CourseList[iRecordNumber].DG1Course = cmbDGF1.Text
  CourseList[iRecordNumber].DG2Course = cmbDGF2.Text
End ' SaveNewRecord()

The following two procedures demonstrate how to read access the contents of the individual fields of a structure:

Public Sub ShowCurrentRecord()
  cmbJGS.Text = CourseList[iCurrentRecord]. Grade
  txbLastName.Text = CourseList[iCurrentRecord].LastName
  dbBirthDate.Value = CourseList[iCurrentRecord].BirthDate
  cmbDGF1.Text = CourseList[iCurrentRecord].DG1Course
  cmbDGF2.Text = CourseList[iCurrentRecord].DG2Course
End ' ShowCurrentRecord()
Public Sub ShowRecords()
  Dim iCount As Integer
 
  TextArea1.Clear
  For iCount = 0 To iRecordNumber
    TextArea1.Insert(CourseList[iCount].Grade & gb.NewLine)
    TextArea1.Insert(CourseList[iCount].LastName & gb.NewLine)
    TextArea1.Insert(Format(CourseList[iCount].BirthDate, "dddd - dd. mmmm yyyy") & gb.NewLine)
    TextArea1.Insert(CourseList[iCount].DG1Course & gb.NewLine)
    TextArea1.Insert(CourseList[iCount].DG2Course & gb.NewLine)
    If iCount < iRecordNumber Then TextArea1.Insert("-------------------------" & gb.NewLine)
  Next ' iCount
  TextArea1.Pos = Len(TextArea1.Text) ' ---> Jump to the last line
 
End ' ShowRecords()
The website uses a temporary session cookie. This technically necessary cookie is deleted when the browser is closed. You can find information on cookies in our privacy policy.
k7/k7.2/k7.2.1/start.txt · Last modified: 05.02.2022 (external edit)

Page Tools