INPUT AND OUTPUT 1 OUTPUT Text Boxes Labels

INPUT AND OUTPUT 1

OUTPUT Text. Boxes & Labels Msg. Box Function/Method Message. Box Class Files (next topic) Printing (later) 2
![Msg. Box (text [, buttons [, caption/title]]) As Msg. Box. Result Msg. Box(“You must Msg. Box (text [, buttons [, caption/title]]) As Msg. Box. Result Msg. Box(“You must](http://slidetodoc.com/presentation_image_h2/645295cfcfa8445c5501a79f708128cc/image-3.jpg)
Msg. Box (text [, buttons [, caption/title]]) As Msg. Box. Result Msg. Box(“You must provide a file name”, , ”File Error”) To learn more about Msg. Boxes, use Help. 3
![Msg. Box (text [, buttons [, caption/title]]) As Msg. Box. Result response = Msg. Msg. Box (text [, buttons [, caption/title]]) As Msg. Box. Result response = Msg.](http://slidetodoc.com/presentation_image_h2/645295cfcfa8445c5501a79f708128cc/image-4.jpg)
Msg. Box (text [, buttons [, caption/title]]) As Msg. Box. Result response = Msg. Box(“Incorrect name-Continue? ”, 4+32, ”File Error”) CType(4+32, Msg. Box. Style), ”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

Msg. Box response = Msg. Box(“Incorrect name-Continue? ”, CType(4+32, Msg. Box. Style), ”File Error”) Instead of using the numerical values, we will use VB constants: response = Msg. Box(“Incorrect name-Continue? ", y, "File Error")) Msg. Box. Style. Yes. No, "File Msg. Box. Style. Yes. No+Msg. Box. Style. Question, Ctype(Msg. Box. Style. Yes. No+Msg. Box. Style. Question, Error") "File Error") Msg. Box. Style), "File Error") Now let’s look at the value for response. 5

Msg. Box response = Msg. Box(“Incorrect name-Continue? ”, CType(Msg. Box. Style. Yes. No+Msg. Box. Style. Question, Msg. Box. Style), "File Error") vb. No) 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
![Message. Box. Show (text [ , caption/title [, buttons [, icon[ , defaultbutton]]]]) Message. Message. Box. Show (text [ , caption/title [, buttons [, icon[ , defaultbutton]]]]) Message.](http://slidetodoc.com/presentation_image_h2/645295cfcfa8445c5501a79f708128cc/image-7.jpg)
Message. Box. Show (text [ , caption/title [, buttons [, icon[ , defaultbutton]]]]) Message. Box. Show("You must provide a file name", "File Error", Message. Box. Buttons. OK) To learn more about Message. Boxes, use Help or see pages 320 -321 in the text. 7

INPUT Textboxes Input. Box Function/Method Files (next topic) 8
![Input. Box ( Prompt As String[, Title As String][, Default. Response As String][, XPos Input. Box ( Prompt As String[, Title As String][, Default. Response As String][, XPos](http://slidetodoc.com/presentation_image_h2/645295cfcfa8445c5501a79f708128cc/image-9.jpg)
Input. Box ( Prompt As String[, Title As String][, Default. Response As String][, XPos As Integer][, YPos As Integer]) As String file. Name = If the user clicks OK, file. Name will be grades. xlsx, and Input. Box("You must. Cancel, provide a file if the user clicks file. Name will bename", the empty"File string. Error") To learn more about Input. Boxes, 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 File. Stream 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 in. File; in. File. open(“measures. txt”); . . . in. File. close(); I/O is one of the areas in which c and c++ are different! Imports System. IO Dim in. File As Stream. Reader in. File = File. Open. Text (“measures. txt”) . . . in. File. 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 out. File; out. File. open(“results. txt”); . . . out. File. close(); Imports System. IO Dim out. File As Stream. Writer out. File = File. Create. Text (“results. txt”) . . . out. File. close() To add to an existing file, you would use File. Append. Text 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 in. File; in. File. open(“measures. txt”); ofstream out. File; Why are out. File. open(“results. txt”); . . . Assume that we have: int in. File >> length; int in. File >> width; int area=length*width; out. File << area << Imports System. IO Dim in. File As Stream. Reader Dim out. File As Stream. Writer in. File = File. Open. Text these needed? (“measures. txt”) out. File = File. Create. Text (“results. txt”). . . length; width; area; Dim length As Integer length = Cint(in. File. Readline) Dim width As Integer width = Cint(in. File. Read. Line) Dim area As Integer area = length * width endl; out. File. Write. Line(area. To. String). . . NOT needed, the Write and Write. Line in. File. close(); methods are “overloaded. ” out. File. 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 in. File As Stream. Reader Dim out. File As Stream. Writer in. File = File. Open. Text (“measures. txt”) out. File = other File. Create. Text There are ways to test for (“results. txt”) EOF, but this is the most readable, and we will use this approach! For i = 1(Not To 3 in. File. End. Of. Stream) Do While length = Cint(infile. Readline) width = Cint(infile. Read. Line) area = length * width out. File. Write. Line(area) Next Loop i Assume that we have: in. File. close() Dim i As Integer out. File. 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, I file am using comma Such If we were a data containing name address delimiters. city state zip, and the output file looking like: why would this be a. CSV poor choice for a delimiter? files are referred to as – 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 285 and 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“ name Anthony Joseph Nowakowski first 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 295 and returns an array of strings, where each element contains a substring of the original string • Split (string [, delimiter. String] [, 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 in. File As Stream. Reader Dim out. File As Stream. Writer in. File = File. Open. Text (“measures. csv”) out. File = File. Create. Text (“results. csv”) Do While (Not in. File. End. Of. Stream) record = infile. Read. Line These create a new CSV file. fields = Split(record, ”, ”) length = Cint(fields(0)) width = Cint(fields(1)) area = length * width out. File. Write(length & ”, ”) out. File. Write(width & ”, ”) out. File. Write. Line(area) Loop in. File. close() out. File. 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 in. File As Stream. Reader in. File = File. Open. Text("GBA. txt") txt. GB. Text = in. File. Read. To. End lbl. GB. Text in. File. Close() 28

Some Other Considerations • What happens if we used the newer format? i. e. no indentation with double-spaced paragraphs. • Instead of using Read. To. End, what would happen if we used Read. Line to read each paragraph? – The tabbed format is easier to implement. 29

he t ad tion e 4 R rip t n c e s De sigm s A Of 30
- Slides: 30