IS 118 Introduction to Development Tools VB Chapter

IS 118 Introduction to Development Tools VB Chapter 06 IS 118 1

Chapter 6: Input/Output Visual Basic. NET Copyright (c) 2003 by Prentice Hall

Objectives l Use the Msg. Box function to determine the user’s response when your program needs a decision l Develop code to handle text files for input and output l Use file dialog boxes to prompt for file paths l Appreciate the need to create and use general procedures IS 118 3

Objectives l Write and use general procedures l Differentiate between the situations when a sub and a function should be written l Determine when a parameter is needed for a general procedure IS 118 4

Input and Output l Input: the process of obtaining data from a source external to the CPU ¡ i. e. , keystrokes and mouse clicks l Output: the process of sending the data in the CPU to an external device ¡ i. e. , sending output to a monitor or printer IS 118 5

The Msg. Box function l Msg. Box ¡ Syntax: l Can can be used to display a message Msg. Box(Prompt [, Buttons] [, Title]) also be used to record a response ¡ Syntax: Variable=Msg. Box(Prompt, [, Buttons] [, Title]) Msg. Box. Style constant determines which buttons are available l Button the user clicks are stored in variable l IS 118 6

Handling Files l Two objects used to handle files ¡ Stream. Reader used to read files ¡ Stream. Writer used to write files l Four step process ¡ Declare an object variable to indicate object type ¡ Create the object ¡ Use object’s methods to perform operations ¡ Close the object IS 118 7

Declaring Object Variables l Two step process ¡ Declare variable name and type l l ¡ Create the object and associate file with variable l l l Dim Phone. File. I as System. IO. Stream. Reader System. IO referred to as namespace Phone. File. I = New System. IO. Stream. Reader(filename) New keyword creates object and associates variable with file name Can be combined into one step ¡ Dim Phone. File. I as New System. IO. Stream. Reader(filename) IS 118 8

Reading Data From a File l l Stream. Reader object used to read from a file Stream. Reader provides several methods ¡ ¡ ¡ l Peek returns the next character, but does not advance Read reads specified number of characters and advances Read. Line reads one line of data starting from current position Read. To. End reads remainder of file starting from current position Close dissociates object from physical file Closing the object after you have finished reading is crucial IS 118 9

Testing for End of File l Eventually, you get to the end of the file ¡ If you try to read more data, you get a runtime error l Use Peek method to test for end of file ¡ Peek l returns the next character in the file If there is no more data, Peek will return -1 l Read more data if Peek method returns something other than -1 IS 118 10

Output with Files l Stream. Writer Object used to write to file ¡ Must specify File. Name to write to ¡ Must specify Append. Mode If file does not exist, it is created l If file does exist and Append. Mode is True, new data written at end of existing file l If file does exist and Append. Mode is False, existing contents will be erased l IS 118 11

Stream. Writer Methods l Write method writes the data to the file l Write. Line method also writes data to file ¡ Adds an “end of line” marker at end l Close method dissociates file from variable l Closing the object after you have written all your data is crucial IS 118 12

File Dialog Boxes l Open. File. Dialog lets user specify file to open for reading l Save. File. Dialog lets user specify where to save file IS 118 13

Common Properties l Filter shows only types of specified file extension l Title displays a string on dialog box title bar l File. Name sets or returns the filename specified by the user l Add. Extension determines whether extension is automatically added IS 118 14

Procedure Types l Event procedure performs statements inside procedure ¡ l Sub performs statements inside procedure ¡ l Triggered by event Invoked when another procedure references it by name Function performs statements inside procedure ¡ ¡ Invoked when another procedure references it by name Returns a value IS 118 15
![Writing a Sub Procedure l Declared with Sub keyword ¡ Syntax: [Private|Public] Sub. Name(Parameter Writing a Sub Procedure l Declared with Sub keyword ¡ Syntax: [Private|Public] Sub. Name(Parameter](http://slidetodoc.com/presentation_image/18d77d177bd1362462f0cfec900aa2c5/image-16.jpg)
Writing a Sub Procedure l Declared with Sub keyword ¡ Syntax: [Private|Public] Sub. Name(Parameter List) [Private|Public] are optional access modifiers l Sub. Name is any valid name l Parameter List is a list of parameters (arguments) passed to the sub l l Ends with End Sub statement IS 118 16

Sub Procedure Modifiers l Access modifiers declare scope of procedure ¡ ¡ l Private procedures recognized only in the current form or class Public procedures accessible to all modules in project Parameters contain data the procedure needs to perform its task ¡ By. Val keyword indicates the actual value of variable is passed to procedure l ¡ If value is changed by procedure, original value of variable is unchanged By. Ref keyword indicates variable memory address is passed to procedure l If value is changed by procedure, memory address is updated IS 118 17

Calling a Sub Procedure Refer to the Sub by name l Pass in required arguments (parameters) l ¡ ¡ l Arguments must be passed in the order given in the Sub header Arguments must be of same type as specified in the Sub header It is possible to pass data by parameter name ¡ Syntax: Sub. Name(Parameter 1: =Argument 1…) l l Parameter 1 represents name of parameter Argument 1 represents argument passed IS 118 18

Terminating a Procedure Before Reaching the End l Exit Sub statement terminates a Sub ¡ Often included inside an If block or loop if you want the procedure to end if a certain condition is met l Return statement also terminates the sub l Control of program returned to calling procedure IS 118 19

Function Procedure Similar to a Sub procedure, except it returns a value l Declared with Function keyword l ¡ l Since a function returns a value, the Function name should appear on left side of an assignment statement at least once ¡ l Syntax: Function Area. Of. Circle(Size as Single) as Single Return statement will also return a value Function is often used as part of an expression ¡ i. e. Pizza. Area = Area. Of. Circle(Pizza. Size) where Area. Of. Circle is a function and Pizza. Size is a parameter IS 118 20

Additional Notes on General Procedures l Use a Function when the value returned by a function will be used like a variable in an expression ¡ Use a Function when there is a need to know whether the actions in the procedure were successful l l Many programming firms require all procedures to be written as Functions, even if no return value is needed, with 1 to indicate success and 0 to indicate failure Procedure names should reflect the actions that take place ¡ ¡ Call a Sub that saves a record Save. Rec Call a Function that calculates and retrieves GPA Calc. GPA IS 118 21

Documentation l Most general procedures are written to handle complex situations. Use comments to document the procedure l Comments should include: ¡ Purposes of the procedure ¡ What is returned if the procedure is a function ¡ A description of required parameters ¡ Assumptions made ¡ The algorithm used if problem is complex IS 118 22

Documentation Example IS 118 23

General Procedure Characteristics l Optional parameters can be omitted when procedure is called ¡ ¡ Specified with Optional keyword All parameters following an optional parameter must be optional Procedures are recursive: they can call themselves l Overloading procedures: two or more procedures with same name l ¡ Must have different parameter list l Can be different in number, type, or both IS 118 24

An Application Example: The Contacts Project l Analyze and define requirements ¡ Read file and inspect records one at a time ¡ Allow user to change or delete existing records or add new ones l Define the user interface ¡ Data fields for entering phone number and name ¡ Buttons to read a record, save the record, clear the fields on the form, and quit ¡ Open file and Save file dialogs IS 118 25

The Visual Interface Copyright (c) 2003 by Prentice Hall 26

An Application Example: The Contacts Project l Designing the Code Structure ¡ Look at procedures that must be executed ¡ Look for common procedures among controls that can be made into general procedures l Decide whether functions or subs are needed ¡ Determine parameters needed for general procedures l Coding the project ¡ Code general procedures, then code event procedures IS 118 27

General Procedures Stream. Reader and Stream. Writer declared as class-level variables Workaround required to clear the text box IS 118 28

Event Procedures Read a record and parse into fields Populate controls. Workaround needed to populate masked edit IS 118 29

Summary l l l Msg. Box can be used to display messages or prompt the user for direction Stream. Reader object opens a file for input Stream. Writer object opens a file for output Close the object when no additional operation is needed VS. NET organized into namespaces ¡ Objects must either be referenced by namespace or namespace imported into project IS 118 30

Summary l l l l File dialogs used to prompt user to specify file name to be opened General procedures divided into Subs and Functions return a value, Subs do not General procedures enhance code reusability In general parameters are passed by position Subs can be terminated with Exit Sub statement Functions can be terminated with Exit Function statement IS 118 31

Summary l Function can be used in an expression l The names of general procedures should be meaningful l All procedures are recursive l A procedure can have optional parameters l More than one procedure can have the same name if they have different parameter lists IS 118 32
- Slides: 32