Presentation is loading. Please wait.

Presentation is loading. Please wait.

IS 1181 IS 118 Introduction to Development Tools VB Chapter 06.

Similar presentations


Presentation on theme: "IS 1181 IS 118 Introduction to Development Tools VB Chapter 06."— Presentation transcript:

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

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

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

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

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

6 IS 1186 The MsgBox function MsgBox can be used to display a message  Syntax: MsgBox(Prompt [,Buttons] [,Title]) Can also be used to record a response  Syntax: Variable=MsgBox(Prompt,[,Buttons] [,Title]) MsgBoxStyle constant determines which buttons are available Button the user clicks are stored in variable

7 IS 1187 Handling Files Two objects used to handle files  StreamReader used to read files  StreamWriter used to write files Four step process  Declare an object variable to indicate object type  Create the object  Use object’s methods to perform operations  Close the object

8 IS 1188 Declaring Object Variables Two step process  Declare variable name and type Dim PhoneFileI as System.IO.StreamReader System.IO referred to as namespace  Create the object and associate file with variable PhoneFileI = New System.IO.StreamReader(filename) New keyword creates object and associates variable with file name Can be combined into one step  Dim PhoneFileI as New System.IO.StreamReader(filename)

9 IS 1189 Reading Data From a File StreamReader object used to read from a file StreamReader provides several methods  Peek returns the next character, but does not advance  Read reads specified number of characters and advances  ReadLine reads one line of data starting from current position  ReadToEnd reads remainder of file starting from current position  Close dissociates object from physical file Closing the object after you have finished reading is crucial

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

11 IS 11811 Output with Files StreamWriter Object used to write to file  Must specify FileName to write to  Must specify AppendMode If file does not exist, it is created If file does exist and AppendMode is True, new data written at end of existing file If file does exist and AppendMode is False, existing contents will be erased

12 IS 11812 StreamWriter Methods Write method writes the data to the file WriteLine method also writes data to file  Adds an “end of line” marker at end Close method dissociates file from variable Closing the object after you have written all your data is crucial

13 IS 11813 File Dialog Boxes OpenFileDialog lets user specify file to open for reading SaveFileDialog lets user specify where to save file

14 IS 11814 Common Properties Filter shows only types of specified file extension Title displays a string on dialog box title bar FileName sets or returns the filename specified by the user AddExtension determines whether extension is automatically added

15 IS 11815 Procedure Types Event procedure performs statements inside procedure  Triggered by event Sub performs statements inside procedure  Invoked when another procedure references it by name Function performs statements inside procedure  Invoked when another procedure references it by name  Returns a value

16 IS 11816 Writing a Sub Procedure Declared with Sub keyword  Syntax: [Private|Public] Sub SubName(Parameter List) [Private|Public] are optional access modifiers SubName is any valid name Parameter List is a list of parameters (arguments) passed to the sub Ends with End Sub statement

17 IS 11817 Sub Procedure Modifiers Access modifiers declare scope of procedure  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  ByVal keyword indicates the actual value of variable is passed to procedure If value is changed by procedure, original value of variable is unchanged  ByRef keyword indicates variable memory address is passed to procedure If value is changed by procedure, memory address is updated

18 IS 11818 Calling a Sub Procedure Refer to the Sub by name Pass in required arguments (parameters)  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: SubName(Parameter1:=Argument1…) Parameter1 represents name of parameter Argument1 represents argument passed

19 IS 11819 Terminating a Procedure Before Reaching the End 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 Return statement also terminates the sub Control of program returned to calling procedure

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

21 IS 11821 Additional Notes on General Procedures 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 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 SaveRec  Call a Function that calculates and retrieves GPA CalcGPA

22 IS 11822 Documentation Most general procedures are written to handle complex situations. Use comments to document the procedure 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

23 IS 11823 Documentation Example

24 IS 11824 General Procedure Characteristics 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 Overloading procedures: two or more procedures with same name  Must have different parameter list Can be different in number, type, or both

25 IS 11825 An Application Example: The Contacts Project 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 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

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

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

28 IS 11828 General Procedures Workaround required to clear the text box StreamReader and StreamWriter declared as class-level variables

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

30 IS 11830 Summary MsgBox can be used to display messages or prompt the user for direction StreamReader object opens a file for input StreamWriter 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

31 IS 11831 Summary File dialogs used to prompt user to specify file name to be opened General procedures divided into Subs and Functions 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

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


Download ppt "IS 1181 IS 118 Introduction to Development Tools VB Chapter 06."

Similar presentations


Ads by Google