200 likes | 366 Views
Lecture Set 12. Sequential Files and Structures Part D - Structures. Objectives. Use structures to store and group data together Structures versus structure variables Structure declarations Accessing structures The with statement More complex data structures.
E N D
Lecture Set 12 Sequential Files and Structures Part D - Structures
Objectives • 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 • 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 • Once declared, variables can be declared having the declared data type
Structures (Syntax) [Public | Friend] Structure name variableDeclarations [procedureDeclarations] End Structure
Structures (Syntax, continued) • The Structure and EndStructure 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 • Use Pascal case for structure names • variableDeclarations contains the structure members • procedureDeclarations contains procedures appearing in the structure
Structures (Example 1) • Declare a structure named Contact with four members Public Structure Contact Public FirstName As String Public LastName As String Public TelephoneNumber As String Public DateAdded As DateTime End Structure
Structures (Example 2) • Declare a structure for cell in a Sudoku Puzzle Grid Public Structure CellType Public Val As Integer Public Potvals As String Public isOriginal as Boolean End Structure
Declaring Structures • 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 • Declaring a Structure block does not declare a variable • 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 CurrentContact As Contact Dim PreviousContact As Contact Dim SudokuGrid(9,9) As cellType
Declaring a Structure Variable (continued) • It's also possible to declare an array of structures • Examples: Public ContactList() As Contact ReDim ContactList(9) As Contact
Storing and Retrieving Data From a Structure • Assignment statements are used to store and retrieve data from a structure • A period (.) separates the structure variable name and the member name • Examples: CurrentContact.FirstName = "Joe" CurrentContact.LastName = "Smith" CurrentContact.Telephone = "775-555-1288" CurrentContact.DateAdded = #3/22/2006# SudokuGrid(i,j).Val = newval
Assignment Statements Using Structures 1 • Structures can be used on both the left and right side of assignment statements • As always, the data types of each side of the expression must match • Examples: PreviousContact.FirstName = _ CurrentContact.FirstName PreviousContact.LastName = _ CurrentContact.LastName PreviousContact.Address = _ CurrentContact.Address PreviousContact.DateAdded = _ CurrentContact.DateAdded
Assignment Statements Using Structures 2 • Unlike arrays, entire structures may be accessed (all fields accessed together) • Example: • Dim CurrentContact As Contact Dim PreviousContact As Contact PreviousContact = CurrentContact • In the third line, the entire four-component structure CurrentContact is assigned to the PreviousContact Structure • The structures of both Structures must be identical
The With Statement (Introduction) • 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 CurrentContact .FirstName = "Joe" .LastName = "Smith" .Telephone = "775-555-1288" .DateAdded = #3/22/2006# End With
Restrictions on the With Statement • Decision-making statements cannot break up a With block • Repetition structures cannot break up a With block
Using Arrays of Structures • 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) • Store data in the first element of the array named ContactList Private ContactList(99) As Contact ContactList(0).FirstName = "Joe" ContactList(0).LastName = "Smith" ContactList(0).Telephone = "775-555-1288" ContactList(0).DateAdded = #3/22/2006#
Processing Delimited Files Using Arrays of Structures • A sequential file can be read into an array of structures • Steps: • Read the first record performing the priming read • In a loop: • Parse and process the current record • Read the next record until there are no more records