1 / 20

Lecture Set 12

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.

oneida
Download Presentation

Lecture Set 12

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture Set 12 Sequential Files and Structures Part D - Structures

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

  3. 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

  4. Structures (Syntax) [Public | Friend] Structure name variableDeclarations [procedureDeclarations] End Structure

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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

  15. The With Statement (Example) With CurrentContact .FirstName = "Joe" .LastName = "Smith" .Telephone = "775-555-1288" .DateAdded = #3/22/2006# End With

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

  17. 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

  18. 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#

  19. 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

  20. Reading One Record from a File to a Structure

More Related