Lecture Set 12 Sequential Files and Structures Part



![Structures (Syntax) [Public | Friend] Structure name variable. Declarations [procedure. Declarations] End Structure Slide Structures (Syntax) [Public | Friend] Structure name variable. Declarations [procedure. Declarations] End Structure Slide](https://slidetodoc.com/presentation_image/3a5f5e4d76ea09f01f9301fa780330d6/image-4.jpg)
















- Slides: 20

Lecture Set 12 Sequential Files and Structures Part D - Structures

Objectives n n n Slide 2 Use structures to store and group data together Structures versus structure variables Structure declarations Accessing structures The with statement More complex data structures

Introduction to Structures n n n A structure is used to store related data items, and groups them together logically A structure can take the place of multiple individual variables The Structure keyword is used to declare a structure n Slide 3 Once declared, variables can be declared having the declared data type
![Structures Syntax Public Friend Structure name variable Declarations procedure Declarations End Structure Slide Structures (Syntax) [Public | Friend] Structure name variable. Declarations [procedure. Declarations] End Structure Slide](https://slidetodoc.com/presentation_image/3a5f5e4d76ea09f01f9301fa780330d6/image-4.jpg)
Structures (Syntax) [Public | Friend] Structure name variable. Declarations [procedure. Declarations] End Structure Slide 4

Structures (Syntax, continued) n n n Slide 5 The Structure and End Structure keywords mark the beginning and end of a structure block The Public and Friend keywords mark the accessibility of the structure name defines the structure name n Use Pascal case for structure names variable. Declarations contains the structure members procedure. Declarations contains procedures appearing in the structure

Structures (Example 1) n Declare a structure named Contact with four members Public Structure Contact Public First. Name As String Public Last. Name As String Public Telephone. Number As String Public Date. Added As Date. Time End Structure Slide 6

Structures (Example 2) n Declare a structure for cell in a Sudoku Puzzle Grid Public Structure Cell. Type Public Val As Integer Public Potvals As String Public is. Original as Boolean End Structure Slide 7

Declaring Structures n n Slide 8 Structures can be declared in a Class or Module block A Structure block can contain a nested Structure block A Structure block cannot appear inside of a procedure block A structure can have members that are themselves structures

Declaring a Structure Variable n Declaring a Structure block does not declare a variable n n n It declares a new data type The syntax to declare a structure variable (of a structured type) is the same as the syntax to declare any other variable Examples: Dim Current. Contact As Contact Dim Previous. Contact As Contact Dim Sudoku. Grid(9, 9) As cell. Type Slide 9

Declaring a Structure Variable (continued) n n It's also possible to declare an array of structures Examples: Public Contact. List() As Contact Re. Dim Contact. List(9) As Contact Slide 10

Storing and Retrieving Data From a Structure n n n Assignment statements are used to store and retrieve data from a structure A period (. ) separates the structure variable name and the member name Examples: Current. Contact. First. Name = "Joe" Current. Contact. Last. Name = "Smith" Current. Contact. Telephone = "775 -555 -1288" Current. Contact. Date. Added = #3/22/2006# Sudoku. Grid(i, j). Val = newval Slide 11

Assignment Statements Using Structures 1 n Structures can be used on both the left and right side of assignment statements n n As always, the data types of each side of the expression must match Examples: Previous. Contact. First. Name = _ Current. Contact. First. Name Previous. Contact. Last. Name = _ Current. Contact. Last. Name Previous. Contact. Address = _ Current. Contact. Address Previous. Contact. Date. Added = _ Current. Contact. Date. Added Slide 12

Assignment Statements Using Structures 2 n n n Slide 13 Unlike arrays, entire structures may be accessed (all fields accessed together) Example: Dim Current. Contact As Contact Dim Previous. Contact As Contact Previous. Contact = Current. Contact In the third line, the entire four-component structure Current. Contact is assigned to the Previous. Contact Structure The structures of both Structures must be identical

The With Statement (Introduction) n n Slide 14 The With statement supplies a shorthand way of referencing structure and class members A With block begins with the With statement and ends with the End With statement

The With Statement (Example) With Current. Contact. First. Name = "Joe". Last. Name = "Smith". Telephone = "775 -555 -1288". Date. Added = #3/22/2006# End With Slide 15

Restrictions on the With Statement n n Slide 16 Decision-making statements cannot break up a With block Repetition structures cannot break up a With block

Using Arrays of Structures n n n Slide 17 It's possible to declare arrays of structures Following the structure variable name, the array subscript appears Following the array subscript, the structure member appears

Using Arrays of Structures (Example) n Store data in the first element of the array named Contact. List Private Contact. List(99) As Contact. List(0). First. Name = "Joe" Contact. List(0). Last. Name = "Smith" Contact. List(0). Telephone = "775 -555 -1288" Contact. List(0). Date. Added = #3/22/2006# Slide 18

Processing Delimited Files Using Arrays of Structures n n A sequential file can be read into an array of structures Steps: n n Read the first record performing the priming read In a loop: n n Slide 19 Parse and process the current record Read the next record until there are no more records

Reading One Record from a File to a Structure Slide 20