INPUT AND OUTPUT 1 OUTPUT TextBoxes & Labels MsgBox Function/Method MessageBox Class Files (next topic) Printing (later) 2 MsgBox MsgBox (text [,buttons [, caption/title]]) As MsgBoxResult MsgBox(“You must provide a file name”,,”File Error”) To learn more about MsgBoxes, use Help. 3 MsgBox MsgBox (text [,buttons [, caption/title]]) As MsgBoxResult response = MsgBox(“Incorrect name-Continue?”, 4+32,”File Error”) CType(4+32,MsgBoxStyle),”File Error”) When casting using CInt, we know the result will be an Integer. However, when using CType, the type is listed as the last parameter. Using numerical values is rather cryptic. 4 MsgBox response = MsgBox(“Incorrect name-Continue?”, CType(4+32,MsgBoxStyle),”File Error”) Instead of using the numerical values, we will use VB constants: response = MsgBox(“Incorrect name-Continue?", y, "File Error")) MsgBoxStyle.YesNo,"File MsgBoxStyle.YesNo+,"File MsgBoxStyle.YesNo+MsgBoxStyle.Question, Ctype(MsgBoxStyle.YesNo+MsgBoxStyle.Question, Error") Error") "File Error") MsgBoxStyle),"File Error") Now let’s look at the value for response. 5 MsgBox response = MsgBox(“Incorrect name-Continue?”, CType(MsgBoxStyle.YesNo+MsgBoxStyle.Question, MsgBoxStyle),"File Error") vbNo) Then If(response = 7) Then ... ... Now we have some code that is more understandable—Yes and No buttons, a Question mark icon, and we are checking for a negative response. 6 MessageBox MessageBox.Show (text [ ,caption/title [,buttons [, icon[ ,defaultbutton]]]]) MessageBox.Show("You must provide a file name", "File Error",MessageBoxButtons.OK) To learn more about MessageBoxes, use Help or see pages 320-321 in the text. 7 INPUT Textboxes InputBox Function/Method Files (next topic) 8 InputBox InputBox ( Prompt As String[, Title As String][, DefaultResponse As String][, XPos As Integer][, YPos As Integer]) As String fileName = If the user clicks OK, fileName will be grades. xlsx, and InputBox("You mustCancel, provide a file if the user clicks fileName will bename", the empty "File string. Error") To learn more about InputBoxes, use Help. 9 Accessing Files 10 Access Methods 1. Sequential Access Method (SAM) 2. Direct (Random) Access Method (DAM) 3. Indexed Sequential Access Method (ISAM) 11 Accessing Files 1. We are going to limit our work with files and only use the Sequential Access Method (SAM) 2. Text Files (which we will discuss) 3. Binary Files (which we will not discuss) 12 Steps needed to transfer data to/from files 1. Include necessary libraries 2. Declare a file stream object 3. Open the file stream 4. Transfer the data 5. Close the file stream 13 SAM with Text Files Using the FileStream Class Before we can transfer data from a text file, we have to know how it is arranged. For our first example, the data are listed one measurement per line. When accessing a file, if no path is specified, then the file must be in the debug subfolder. 14 C++ VB #include <fstream.h> ifstream inFile; inFile.open(“measures.txt”); . . . inFile.close(); I/O is one of the areas in which c and c++ are different! Imports System.IO Dim inFile As StreamReader inFile = File.OpenText (“measures.txt”) . . . inFile.close() This will cause an error if the file does not exist. We will address trapping errors or catching exceptions in the next slide show. 15 C++ VB #include <fstream.h> ofstream outFile; outFile.open(“results.txt”); . . . outFile.close(); Imports System.IO Dim outFile As StreamWriter outFile = File.CreateText (“results.txt”) . . . outFile.close() To add to an existing file, you would use File.AppendText 16 Let’s put the two previous slides together and create a program the reads data from a file, performs a calculation, and writes the result to another file. 17 C++ VB #include <fstream.h> ifstream inFile; inFile.open(“measures.txt”); ofstream outFile; Why are outFile.open(“results.txt”); . . . Assume that we have: int inFile >> length; int inFile >> width; int area=length*width; outFile << area << . . . inFile.close(); outFile.close(); Imports System.IO Dim inFile As StreamReader Dim outFile As StreamWriter inFile = File.OpenText these needed? (“measures.txt”) outFile = File.CreateText (“results.txt”) . . . length; width; area; Dim length As Integer length = Cint(inFile.Readline) Dim width As Integer width = Cint(inFile.ReadLine) Dim area As Integer area = length * width endl; outFile.WriteLine(area.ToString) . . . NOT needed, the Write and WriteLine inFile.close() methods are “overloaded.” outFile.close() What other code must we add to this program? 18 Although we could use a For-Loop why is this inappropriate? Imports System.IO . . . Dim inFile As StreamReader Dim outFile As StreamWriter inFile = File.OpenText (“measures.txt”) outFile = File.CreateText (“results.txt”) For i = 1 (inFile.Peek To 3 Do While <> -1) length = Cint(infile.Readline) width = Cint(infile.ReadLine) area = length * width outFile.WriteLine(area) Next Loop i Assume that we have: inFile.close() Dim i As Integer outFile.close() 19 How Are Imported Text Files Delimited? 20 Although the previous example works, I think of the input file as: Space delimited Tab delimited Comma delimited Unlike theusing text, am using comma delimiters. If we were a Idata file containing name address city stateSuch zip, and the output file looking like: why would to thisas be aCSV poor choice for a delimiter? files are referred – Comma Separated Values. Let’s look at the code necessary to use a CSV file. 21 Before writing the code we need to examine a method and a function • The Split method is defined on page 281and returns an array of strings, where each element contains a substring of the original string • Split (split character) … name.Split (CChr(" ")) Strings are usually stored with a string count header and then the string. For example “Tony” is stored as 4 T o n y and “Z” is stored as 1 Z . The character Z is stored as just Z . Consequently, we must cast the string into a character. In a like manner, if we are using a space “ “, we must cast it into a character. 22 Consider the following example: Dim name, first, middle, last As String Dim fields(2) As String name Anthony Joseph Nowakowski first name = "Anthony Joseph Nowakowski“ Anthony fields = name.Split(CChar(" ")) middle first = fields(0) middle = fields(1) last = fields(2) Joseph last Nowakowski fields Anthony Joseph Nowakowski 23 Before writing the code we need to examine a method and a function • The Split function is defined on page 291 and returns an array of strings, where each element contains a substring of the original string • Split (string [,delimiterString] [,limit]) … Split(name) If the delimiter is omitted, then a space is used. If the limit is omitted then, all substrings are returned. 24 Before writing the code we need to examine a method and a function Consider the following example: Dim name, first, middle, last As String Dim fields(2) As String name = "Anthony Joseph Nowakowski" fields = Split(name) first = fields(0) middle = fields(1) last = fields(2) 25 We now modify the code for the CSV example Imports System.IO . . . Dim record, fields(1) As String Dim inFile As StreamReader Dim outFile As StreamWriter inFile = File.OpenText (“measures.csv”) outFile = File.CreateText (“results.csv”) Do While (inFile.Peek <> -1) record = infile.ReadLine These create a new CSV file. fields = Split(record,”,”) length = Cint(fields(0)) width = Cint(fields(1)) area = length * width outFile.Write(length & ”,”) outFile.Write(width & ”,”) outFile.WriteLine(area) Loop inFile.close() outFile.close() 26 Our final text example involves the Gettysburg Address Using the “traditional” format. i.e. paragraphs indented and no doublespacing between paragraphs. 27 Our final text example involves the Gettysburg Address Text Box or Label? Imports System.IO . . . Dim inFile As StreamReader inFile = File.OpenText("GBA.txt") txtGB.Text = inFile.ReadToEnd lblGB.Text inFile.Close() 28 Some Other Considerations • What happens if we used the newer format? i.e. no indentation with double-spaced paragraphs. • Instead of using ReadToEnd, what would happen if we used ReadLine to read each paragraph? – The tabbed format is easier to implement. 29 30