230 likes | 361 Views
ITDEV 130 Chapter 10 Structures and Sequential Access Files. Lesson A: Create a Structure. Declare a structure variable. Pass a structure variable to a procedure. Create and manipulate a one-dimensional array of structures. Syntax Structure structureName
E N D
ITDEV 130 Chapter 10 Structures and Sequential Access Files
Lesson A: • Create a Structure. • Declare a structure variable. • Pass a structure variable to a procedure. • Create and manipulate a one-dimensional array of structures.
Syntax Structure structureName Public memberVariableNameAsdataType [Public memberVariableNameAs dataType] End Structure
Example Structure Account Public strID As String Public dblBeginingBal As Double Public dblDeposits As Double Public dblWithdrawals As Double End Structure
Declaring a Structure Variable {Dim | Private} structureVariableNameAsstructureName Dim Checking As Account
Using a member variable Checking.AcBeginingBal = 2000.00 lblAccountID.Text = Checking.AcID
Passing a Structure to a Procedure Previously: Public Sub btnCalc(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCalc.Click Dim strlID As String Dim dblBeginingBal As Double Dim dblDeposits As Double Dim dblWithdrawals As Double AcBeginingBal = NewBal(dblBeginingBal,dblDeposits,dblWithdrawal) End Sub Public Function NewBal(ByValdblBegBal As Double,ByValdblDep As Double,ByValdblWith As Double) As Double return dblBegBal + dblDep – dblWith End Function
With Structure: Structure Account Public strID As String Public dblBeginingBal As Double Public dblDeposits As Double Public dblWithdrawals As Double End Structure Public Sub btnCalc(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCalc.Click Dim Checking As Account Checking.strID = “Checking” Checking.dblBeginingBal = NewBal(Checking) End Sub • Public Function NewBal(ByVal HA As Account) As Double • Return HA .dblBegBal + HA.dblDep– HA.dblWith • End Function
Array of Structure Variables Structure Account Public strID As String Public dblBeginingBal As Double Public dblDeposits As Double Public dblWithdrawals As Double End Structure Public Sub btnCalc(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCalc.Click Dim MyAccounts(4) As Account MyAccount(1).strID = “Checking” MyAccounts(1).dblBeginingBal = NewBal(MyAccounts(1)) End Sub Public Function NewBal(ByVal HA As Account) As Double Return HA .dblBegBal + HA .dblDep – HA .dblWith End Function
Lesson B: • Types of files. • Open and close a sequential file. • Write data to a sequential file. • Read data from a sequential file. • Determine whether a sequential access file exists. • Test for the end of a sequential file.
Sequential Access Files– Early file type. 256 characters per record. Fields are delimited. Record terminated with a LF/CR. Migration from card data. Start at the beginning and move record by record through the end. Can be opened for Append Either Read or Write, not both at same time.
Direct or Random Access Files- • Records may be Read or Written. • Read/Write pointer may be moved to any record (hence direct or random). • Fixed record/field sizes.
Binary Files- Really Direct Files typically with some type of media data (pics, video, sound).
Database Files- Data is structured based on either a hierarchical, network, or relational schema. Database provides tools to: Handel SQL queries. Create & manage the Database. Provide Ad-hoc reporting.
Writing data to an output file using sequential access: In Visual Basic we use something called a SreamWriter object to write data to an output file. Declaring a StreamWriter variable: {Dim|Private} streamWriterVariable name AsIO.StreamWriter Dim outFile As IO.StreamWriter
Creating a StreamWriter object by opening a sequential access file: IO.File.Method(filename) outFile = IO.File.CreateText(“Student.dat”) opens the Student.dat file for output; creates a SteramWriter object and assignsd it to the outfile variable. outFile = IO.File.AppendText(“MyFile.dat”) opens the report.dat file for a\append; creates a StreamWriter object and assigns it to the outFile variable. Note: When the directory is not qualified in the file name, the default location for the file will be the same directory that the .exe file ran from.
Writing data. outFile.Write(“Hello”) – no cr/lf. outFile.WriteLine(“Hello”) – adds cr/lf to end of data.
Reading Data From A Sequential Access File: Syntax {Dim|Private} streamReaderVariableNameAs IO.StreamReader Dim inFile As IO.StreamReader
Creating a StreamReader object by opening a sequential access file Syntax IO.File.OpenText(filename) inFile = IO.File.OpenText(“Student.dat”)
Determining whether a file exists IO.File.Exists(filename) If IO.File.Exists(“Student.dat”) Then ….
Reading data from a sequential access file Syntax straemReaderVariableName.ReadLine strHldData = inFile.ReadLine
Reading data from a file until the end of file is reached: • Do Until inFile.Peek = -1 • strHldData = inFile.ReadLine • do something with the data • Loop