ch03

86
Chapter 3 - VB 2008 by Schneider 1 Chapter 3 – Variables, Input, and Output 3.1 Numbers 3.2 Strings 3.3 Input and Output

Upload: ahmad

Post on 21-Nov-2015

5 views

Category:

Documents


0 download

DESCRIPTION

سلايدات مادة البرمجة بلغة بيسك المرئية

TRANSCRIPT

  • Chapter 3 - VB 2008 by Schneider*Chapter 3 Variables, Input, and Output3.1 Numbers3.2 Strings3.3 Input and Output

    Chapter 3 - VB 2008 by Schneider

  • Chapter 3 - VB 2008 by Schneider*3.1 NumbersArithmetic Operations Variables Incrementing the Value of a Variable Built-In Functions: Math.SqrtIntMath.Round

    Chapter 3 - VB 2008 by Schneider

  • Chapter 3 - VB 2008 by Schneider*Numbers continuedThe Integer Data TypeMultiple Declarations Parentheses Three Types of ErrorsThe Error List Window

    Chapter 3 - VB 2008 by Schneider

  • Chapter 3 - VB 2008 by Schneider*Arithmetic OperationsNumbers are called numeric literalsFive arithmetic operations in Visual Basic+ addition- subtraction* multiplication/ division^ exponentiation

    Chapter 3 - VB 2008 by Schneider

  • Chapter 3 - VB 2008 by Schneider*Numeric Expressions2 + 33 * (4 + 5)2 ^ 3

    Chapter 3 - VB 2008 by Schneider

  • Chapter 3 - VB 2008 by Schneider*Displaying NumbersLet n be a number or a numeric expression.

    The statement lstBox.Items.Add(n)displays the value of n in the list box.

    Chapter 3 - VB 2008 by Schneider

  • Chapter 3 - VB 2008 by Schneider*Example 1: Form

    Chapter 3 - VB 2008 by Schneider

  • Chapter 3 - VB 2008 by Schneider*Example 1: Code and OutputPrivate Sub btnCompute_Click (...) Handles btnCompute.Click lstResults.Items.Add(5) lstResults.Items.Add(2 * 3) lstResults.Items.Add((2 ^ 3) 1)End SubOutput 5in list 6box 7

    Chapter 3 - VB 2008 by Schneider

  • Chapter 3 - VB 2008 by Schneider*Numeric VariableA numeric variable is a name to which a number can be assigned.

    Examples:

    speeddistanceinterestRatebalance

    Chapter 3 - VB 2008 by Schneider

  • Chapter 3 - VB 2008 by Schneider*VariablesDeclaration:Dim speed As Double

    Variable nameData typeAssignment:speed = 50

    Chapter 3 - VB 2008 by Schneider

  • Chapter 3 - VB 2008 by Schneider*InitializationNumeric variables are automatically initialized to 0:Dim varName As Double

    To specify a nonzero initial valueDim varName As Double = 50

    Chapter 3 - VB 2008 by Schneider

  • Chapter 3 - VB 2008 by Schneider*Numeric ExpressionsNumeric variables can be used in numericexpressions.

    Dim balance As Double = 1000lstBox.Items.Add(1.05 * balance) Output: 1050

    Chapter 3 - VB 2008 by Schneider

  • Chapter 3 - VB 2008 by Schneider*Assignment StatementDim numVar1 As Double = 5Dim numVar2 As Double = 4numVar1 = 3 * numVar2lstBox.Items.Add(numVar1)Output: 12

    Chapter 3 - VB 2008 by Schneider

  • Chapter 3 - VB 2008 by Schneider*IncrementingTo add 1 to the numeric variable varvar = var + 1Or as a shortcutvar += 1Or as a generalizationvar += numeric expression

    Chapter 3 - VB 2008 by Schneider

  • Chapter 3 - VB 2008 by Schneider*Built-in FunctionsFunctions return a valueMath.Sqrt(9) returns 3Math.Sqrt(0) returns 0Int(9.7) returns 9Int(2.3) returns 2Math.Round(2.7) is 3Math.Round(2.5) is 2

    Chapter 3 - VB 2008 by Schneider

  • Chapter 3 - VB 2008 by Schneider*Integer Data TypeVariables of type Double can be assigned both whole numbers and numbers with decimals.The statement Dim varName As Integer declares a numeric variable that can only be assigned whole number values between about -2 billion and 2 billion.

    Chapter 3 - VB 2008 by Schneider

  • Chapter 3 - VB 2008 by Schneider*Multiple DeclarationsDim a, b As Double

    Two other types of multiple-declaration statements areDim a As Double, b As IntegerDim c As Double = 2, b As Integer = 5

    Chapter 3 - VB 2008 by Schneider

  • Chapter 3 - VB 2008 by Schneider*ParenthesesParentheses should be used liberally in numeric expressions.In the absence of parentheses, the operations are carried out in the following order: ^, * and /, + and -.

    Chapter 3 - VB 2008 by Schneider

  • Chapter 3 - VB 2008 by Schneider*Three Types of ErrorsSyntax errorRun-time errorLogic error

    Chapter 3 - VB 2008 by Schneider

  • Chapter 3 - VB 2008 by Schneider*Some Types of Syntax ErrorsMisspellings lstBox.Itms.Add(3)Omissions lstBox.Items.Add(2 + )Incorrect punctuation Dim m; n As Integer

    Chapter 3 - VB 2008 by Schneider

  • Chapter 3 - VB 2008 by Schneider*A Type of Run-time ErrorOverflow error

    Dim numVar As Integer = 1000000numVar = numVar * numVar

    Chapter 3 - VB 2008 by Schneider

  • Chapter 3 - VB 2008 by Schneider*A Logical ErrorDim average As DoubleDim m As Double = 5Dim n As Double = 10average = m + n / 2

    Value of average will be 10. Should be 7.5.

    Chapter 3 - VB 2008 by Schneider

  • Chapter 3 - VB 2008 by Schneider*Error List WindowDim m; n As DoublelstResults.Items.Add(5lstResults.Items.Add(a)

    Chapter 3 - VB 2008 by Schneider

  • Chapter 3 - VB 2008 by Schneider*3.2 StringsVariables and StringsOption Explicit and Option Strict Using Text Boxes for Input and OutputConcatenation String Properties and Methods:

    LengthToUpperTrimToLowerIndexOfSubstring

    Chapter 3 - VB 2008 by Schneider

  • Chapter 3 - VB 2008 by Schneider*Strings continuedAuto CorrectionThe Empty String Initial Value of a String Widening and NarrowingInternal DocumentationLine-Continuation CharacterScope of a Variable

    Chapter 3 - VB 2008 by Schneider

  • Chapter 3 - VB 2008 by Schneider*String LiteralA string literal is a sequence ofcharacters surrounded by quotation marks.Examples:"hello""123-45-6789""#ab cde?"

    Chapter 3 - VB 2008 by Schneider

  • Chapter 3 - VB 2008 by Schneider*String VariableA string variable is a name to which astring value can be assigned.

    Examples:countryssnwordfirstName

    Chapter 3 - VB 2008 by Schneider

  • Chapter 3 - VB 2008 by Schneider*String VariableDeclaration:Dim firstName As String

    Variable nameData typeAssignment:firstName = "Fred"

    Chapter 3 - VB 2008 by Schneider

  • Chapter 3 - VB 2008 by Schneider*String VariableYou can declare a string variable and assign it a value at the same time.

    Dim firstName As String = "Fred"

    Chapter 3 - VB 2008 by Schneider

  • Chapter 3 - VB 2008 by Schneider*Add MethodLet str be a string literal or variable. Then, lstBox.Items.Add(str) displays the value of str in the list box.

    Chapter 3 - VB 2008 by Schneider

  • Chapter 3 - VB 2008 by Schneider*String VariableYou can assign the value of one stringvariable to another.Dim strVar1 As String = "Hello"Dim strVar2 As String = "Goodbye"strVar2 = strVar1lstOutput.Items.Add(strVar2)Output: Hello

    Chapter 3 - VB 2008 by Schneider

  • Chapter 3 - VB 2008 by Schneider*Variables and StringsPrivate Sub btnDisplay_Click(...) Handles btnDisplay.Click Dim president As String president = "George Washington" lstOutput.Items.Add("president") lstOutput.Items.Add(president)End Sub

    Output: president George Washington

    Chapter 3 - VB 2008 by Schneider

  • Chapter 3 - VB 2008 by Schneider*Option Strict Visual Basic allows numeric variables to be assigned strings and vice versa, a poor programming practice.To prevent such assignments, set Option Strict to On in the Options dialog box.

    Chapter 3 - VB 2008 by Schneider

  • Chapter 3 - VB 2008 by Schneider*Option Strict -continuedSelect Options from the Tools menuIn left pane, expand Projects and SolutionSelect VB DefaultsSet Option Strict to On

    Chapter 3 - VB 2008 by Schneider

  • Chapter 3 - VB 2008 by Schneider*Using Text Boxes for Input and OutputThe contents of a text box is always a string

    Input example strVar = txtBox.Text

    Output example txtBox.Text = strVar

    Chapter 3 - VB 2008 by Schneider

  • Chapter 3 - VB 2008 by Schneider*Data ConversionBecause the contents of a text box is always a string, sometimes you must convert the input or output.dblVar = CDbl(txtBox.Text)

    txtBox.Text = CStr(numVar)Converts a String to a DoubleConverts a number to a string

    Chapter 3 - VB 2008 by Schneider

  • Chapter 3 - VB 2008 by Schneider*Auto Correction

    Chapter 3 - VB 2008 by Schneider

  • Chapter 3 - VB 2008 by Schneider*With Option Strict OnDim dblVar As Double, intVar As IntegerDim strVar As String

    Not Valid:Replace with:intVar = dblVarintVar = CInt(dblVar)dblVar = strVar dblVar = CDbl(strVar)strVar = intVarstrVar = CStr(intVar)

    Chapter 3 - VB 2008 by Schneider

  • Chapter 3 - VB 2008 by Schneider*Concatenation Combining two strings to make a new string

    quote1 = "We'll always "quote2 = "have Paris."quote = quote1 & quote2txtOutput.Text = quote & " - Humphrey Bogart"

    DisplaysWe'll always have Paris. - Humphrey Bogart

    Chapter 3 - VB 2008 by Schneider

  • Chapter 3 - VB 2008 by Schneider*AppendingTo append str to the string variable varvar = var & str

    Or as a shortcutvar &= str

    Chapter 3 - VB 2008 by Schneider

  • Chapter 3 - VB 2008 by Schneider*Appending ExampleDim var As String = "Good"var &= "bye"txtBox.Text = var

    OUTPUT: GoodbyeCan you concatenate a string witha number and concatenate numbers? yes, the result will be string

    Chapter 3 - VB 2008 by Schneider

  • Chapter 3 - VB 2008 by Schneider*String Properties and Methods "Visual".Length is 6. "Visual".ToUpper is VISUAL."123 Hike".Length is 8. "123 Hike".ToLower is 123 hike."a" & " bcd ".Trim & "efg" is abcdefg.

    Chapter 3 - VB 2008 by Schneider

  • Chapter 3 - VB 2008 by Schneider*Positions in a String Positions of characters in a string are numbered 0, 1, 2, .Consider the string Visual Basic.Position 0: VPosition 1: iPosition 7: BSubstring al begins at position 4

    Chapter 3 - VB 2008 by Schneider

  • Chapter 3 - VB 2008 by Schneider*Substring Method Let str be a string.str.Substring(m, n) is the substring of length n, beginning at position m in str.

    Visual Basic.Substring(2, 3) is suaVisual Basic.Substring(0, 1) is V

    Chapter 3 - VB 2008 by Schneider

  • Chapter 3 - VB 2008 by Schneider*IndexOf Method Let str1 and str2 be strings. str1.IndexOf(str2)is the position of the first occurrence of str2 in str1.(Note: Has value -1 if str2 is not a substring of str1.)"Visual Basic".IndexOf("is") is 1."Visual Basic".IndexOf("si") is 9."Visual Basic".IndexOf("ab") is -1."Mississippi".IndexOf(ss,3) is 5.

    Chapter 3 - VB 2008 by Schneider

  • Chapter 3 - VB 2008 by Schneider*The Empty String The string "", which contains no characters, is called the empty string or the zero-length string.The statement lstBox.Items.Add("") skips a line in the list box. The contents of a text box can be cleared with either the statement txtBox.Clear() or the statement txtBox.Text = ""

    Chapter 3 - VB 2008 by Schneider

  • Chapter 3 - VB 2008 by Schneider*Initial Value of a String By default the initial value is NothingStrings can be given a different initial value as follows:

    Dim name As String = "Fred"

    Chapter 3 - VB 2008 by Schneider

  • Chapter 3 - VB 2008 by Schneider*Widening and NarrowingWidening: assigning an Integer value to a Double variableWidening always works. (Every Integer is a Double.)No conversion function needed.

    Narrowing: assigning a Double value to an Integer variableNarrowing might not work. (Not every Double is an Integer.)Narrowing requires Cint.

    Strings can be given a different initial value as follows

    Chapter 3 - VB 2008 by Schneider

  • Chapter 3 - VB 2008 by Schneider*CommentsPrivate Sub btnCompute_Click (...) Handles btnCompute.Click 'Calculate the balance in an account Dim rate As Double 'Annual rate of interest Dim curBalance As Double 'Current balance

    Chapter 3 - VB 2008 by Schneider

  • Chapter 3 - VB 2008 by Schneider*Internal DocumentationOther people can easily understand the program.You can understand the program when you read it later.Long programs are easier to read because the purposes of individual pieces can be determined at a glance.

    Chapter 3 - VB 2008 by Schneider

  • Chapter 3 - VB 2008 by Schneider*Line-Continuation CharacterA long line of code can be continued on another line by using an underscore (_) preceded by a space

    msg = "I'm going to make " & _ "him an offer he can't refuse."

    Chapter 3 - VB 2008 by Schneider

  • Chapter 6 - VB 2008 by Schneider*ScopeThe scope of a variable is the portion of the program that can refer to it.Variables declared inside an event procedure are said to have local scope and are only available in the event procedure in which they are declared.

    Chapter 6 - VB 2008 by Schneider

  • Chapter 6 - VB 2008 by Schneider*ScopeVariables declared outside an event procedure are said to have class-level scope and are available to every event procedure.Usually declared after Public Class formName (Declarations section of Code Editor.)

    Chapter 6 - VB 2008 by Schneider

  • Chapter 3 - VB 2008 by Schneider*Automatic ColorizationComments greenString literals maroonKeywords blueNote: Keywords are words such as Sub,Handles, Private, With, and End that havespecial meaning in Visual Basic. Theycannot be used as variable names.

    Chapter 3 - VB 2008 by Schneider

  • Chapter 3 - VB 2008 by Schneider*3.3 Input and OutputFormatting Output with Format FunctionsFormatting Output with ZonesReading Data from FilesGetting Input from an Input Dialog BoxUsing a Message Dialog Box for OutputUsing a Masked Text Box for Input

    Chapter 3 - VB 2008 by Schneider

  • Chapter 3 - VB 2008 by Schneider*Formatting Output with Format Functions

    FunctionString ValueFormatNumber(12345.628, 1)12,345.6FormatCurrency(12345.628, 2)$12,345.63FormatPercent(0.183, 0)18%FormatPercent(0.185, 2)18.50%

    Chapter 3 - VB 2008 by Schneider

  • Chapter 3 - VB 2008 by Schneider*Formatting Output with ZonesUse a fixed-width font such as Courier New Divide the characters into zones with a format string.Dim fmtStr As String = "{0, 15}{1, 10}{2, 8}"lstOutput.Items.Add(String.Format(fmtStr, _ data0, data1, data2))

    Chapter 3 - VB 2008 by Schneider

  • Chapter 3 - VB 2008 by Schneider*Formatting Output with Zones ExampleDim fmtStr As String = "{0, 15}{1, 10}"lstOutput.Items.Add(String.Format(fmtStr, _ Name, Major))lstOutput.Items.Add(String.Format(fmtStr, _ Mohammed, MIS))lstOutput.Items.Add(String.Format(fmtStr, _ Ahmed, CIS))

    Chapter 3 - VB 2008 by Schneider

  • Chapter 3 - VB 2008 by Schneider*Formatting Output with ZonesDim fmtStr As String = "{0, -15}{1, 10}{2, 8}"lstOutput.Items.Add(String.Format(fmtStr, _ data0, data1, data2))Here, 15 was preceded by a minus sign. Thisproduces left justification in 0th zone. There willbe right justification in the other two zones.

    Chapter 3 - VB 2008 by Schneider

  • Chapter 3 - VB 2008 by Schneider*Zone Formatting Symbols Dim fmtStr As String = "{0,15:N1}{1,10:C2}{2,8:P0}"

    Symbols: N, C, and PEffect on zone:NrFormatNumber(data, r):CrFormatCurrency(data, r):PrFormatPercent(data, r)

    Chapter 3 - VB 2008 by Schneider

  • Chapter 3 - VB 2008 by Schneider*Reading Data from FilesData can be stored in text files and accessed with a StreamReader object.We assume that the text files have one piece of data per line.

    Chapter 3 - VB 2008 by Schneider

  • Chapter 3 - VB 2008 by Schneider*Sample File: PAYROLL.TXTMike Jones 9.3535John Smith10.7533NameHourly wageNumber of hours worked

    Chapter 3 - VB 2008 by Schneider

  • Chapter 3 - VB 2008 by Schneider*Steps to Use StreamReaderExecute a statement of the form Dim readerVar As IO.StreamReader = _ IO.File.OpenText(filespec) or the pair of statements Dim readerVar As IO.StreamReader readerVar = IO.File.OpenText(filespec)

    Chapter 3 - VB 2008 by Schneider

  • Chapter 3 - VB 2008 by Schneider*Steps to Use StreamReaderRead items of data in order, one at a time,from the file with the ReadLine method. strVar = readerVar.ReadLineAfter the desired items have been read fromthe file, terminate the communications link readerVar.Close()

    Chapter 3 - VB 2008 by Schneider

  • Chapter 3 - VB 2008 by Schneider*Example using StreamReaderDim name As StringDim wage, hours As DoubleDim sr As IO.StreamReader = _ IO.File.OpenText("PAYROLL.TXT")name = sr.ReadLinewage = CDbl(sr.ReadLine)hours = CDbl(sr.ReadLine)lstBox.Items.Add(name & ": " & wage * hours)OUTPUT: Mike Jones: 327.25

    Programs bin folder/debug subfolder

    Chapter 3 - VB 2008 by Schneider

  • Chapter 3 - VB 2008 by Schneider*Comment on ExampleConsiderlstBox.Items.Add(name & ": " & wage * hours)The ampersand automatically convertedwage * hours into a string before concatenating.We didnt have to convert wage * hours with CStr.

    Chapter 3 - VB 2008 by Schneider

  • Chapter 8 - VB 2008 by Schneider*Chapter 8 Sequential Files8.1 Sequential Files8.2 Using Sequential Files

    Chapter 8 - VB 2008 by Schneider

  • Chapter 8 - VB 2008 by Schneider*Section 8.1 Sequential FilesCreating a Sequential FileAdding Items to a Sequential FileStructured Exception Handling

    Chapter 8 - VB 2008 by Schneider

  • Chapter 8 - VB 2008 by Schneider*Sequential FilesA sequential file consists of data stored in a text file on disk.May be created with the Visual Basic IDEMay also be created programmatically from Visual Basic

    Chapter 8 - VB 2008 by Schneider

  • Chapter 8 - VB 2008 by Schneider*Creating a Sequential FileChoose a filename may contain up to 215 charactersSelect the path for the folder to contain this fileExecute a statement like the following:Dim sw As IO.StreamWriter = IO.File.CreateText(filespec) (Opens a file for output.)

    Chapter 8 - VB 2008 by Schneider

  • Chapter 8 - VB 2008 by Schneider*Creating a Sequential FilePlace lines of data into the file with statements of the form: sw.WriteLine(datum)Close the file: sw.Close()Note: If no path is given for the file, it willbe placed in the Debug subfolder of bin.

    Chapter 8 - VB 2008 by Schneider

  • Chapter 8 - VB 2008 by Schneider*ExamplePrivate Sub btnCreateFile_Click(...) _ Handles btnCreateFile.Click Dim sw As IO.StreamWriter = IO.File.CreateText("PAYROLL.TXT") sw.WriteLine("Mike Jones") 'Name sw.WriteLine(9.35) 'Wage sw.WriteLine(35) Hours worked sw.WriteLine("John Smith") sw.WriteLine(10.75) sw.WriteLine(33) sw.Close()End Sub

    Chapter 8 - VB 2008 by Schneider

  • Chapter 8 - VB 2008 by Schneider*File: PAYROLL.TXTMike Jones 9.3535John Smith10.7533

    Chapter 8 - VB 2008 by Schneider

  • Chapter 8 - VB 2008 by Schneider*Caution If an existing file is opened for output, Visual Basic will erase the existing file and create a new one.

    Chapter 8 - VB 2008 by Schneider

  • Chapter 8 - VB 2008 by Schneider*Adding Items to a Sequential FileExecute the statement Dim sw As IO.StreamWriter = IO.File.AppendText(filespec) where sw is a variable name and filespec identifies the file. Place data into the file with the WriteLine method.After all the data have been recorded into the file, close the file with the statement sw.Close()

    Chapter 8 - VB 2008 by Schneider

  • Chapter 8 - VB 2008 by Schneider*IO.File.AppendTextWill add data to the end of an existing fileIf a file does not exist, the method will create it.

    Chapter 8 - VB 2008 by Schneider

  • Chapter 8 - VB 2008 by Schneider*Sequential File ModesCreateText open for outputOpenText open for inputAppendText open for appendA file should not be opened in two different modes at the same time.

    Chapter 8 - VB 2008 by Schneider

  • Chapter 8 - VB 2008 by Schneider*Imports System.IOSimplifies programs that have extensive file handling.Place the statement Imports System.IO at the top of the Code Editor, before the Class frmName statement. Then, there is no need to insert the prefix IO. before the words StreamReader, StreamWriter, and File.

    Chapter 8 - VB 2008 by Schneider

  • Chapter 3 - VB 2008 by Schneider*Getting Input from an Input Dialog BoxstringVar = InputBox(prompt, title)fileName = InputBox("Enter the name " _ & "of the file containing the " & _ "information.", "Name of File")

    TitlePrompt

    Chapter 3 - VB 2008 by Schneider

  • Chapter 3 - VB 2008 by Schneider*Using a Message Dialog Box for OutputMessageBox.Show(prompt, title)MessageBox.Show("Nice try, but no cigar.", _ "Consolation")

    TitlePrompt

    Chapter 3 - VB 2008 by Schneider

  • Chapter 2 - VB 2008 by Schneider*Masked Text Box ControlSimilar to an ordinary text box, but has a Mask property that restricts what can be typed into the masked text box.Tasks button

    Chapter 2 - VB 2008 by Schneider

  • Chapter 3 - VB 2008 by Schneider*Masked Text Box ControlClick the Tasks button to reveal Set Mask property.Click Set Mask to invoke Input Mask dialog box.

    Chapter 3 - VB 2008 by Schneider

  • Chapter 3 - VB 2008 by Schneider*Input Mask Dialog Box

    Chapter 3 - VB 2008 by Schneider

  • Chapter 3 - VB 2008 by Schneider*Mask A Mask setting is a sequence of characters, with 0, L, and & having special meanings.0 Placeholder for a digit.L Placeholder for a letter.& Placeholder for a character

    Chapter 3 - VB 2008 by Schneider

  • Chapter 3 - VB 2008 by Schneider*Sample Masks State abbreviation: LLPhone number: 000-0000Social Security Number: 000-00-0000License plate: &&&&&&

    Chapter 3 - VB 2008 by Schneider

  • Chapter 3 - VB 2008 by Schneider*Importing a Text File Highlight program name in Solution ExplorerClick on Add Existing Item in Project menuLocate text file and double-click on it. (File name will appear in Solution Explorer.)Move file to Debug subfolder of bin folder.

    Chapter 3 - VB 2008 by Schneider

    ***********declares a variable named varName to be of type Double. Actually, the Dim statementcauses the computer to set aside a location in memory with the name varName. SincevarName is a numeric variable, the Dim statement also places the number zero in thatmemory location. (We say that zero is the initial value or default value of the variable.)

    *declares a variable named varName to be of type Double. Actually, the Dim statementcauses the computer to set aside a location in memory with the name varName. SincevarName is a numeric variable, the Dim statement also places the number zero in thatmemory location. (We say that zero is the initial value or default value of the variable.)

    *declares a variable named varName to be of type Double. Actually, the Dim statementcauses the computer to set aside a location in memory with the name varName. SincevarName is a numeric variable, the Dim statement also places the number zero in thatmemory location. (We say that zero is the initial value or default value of the variable.)

    **************************************************1. Execute a statement of the formDim readerVar As IO.StreamReaderA StreamReader is an object from the Input/Output class that can read a stream ofcharacters coming from a disk or coming over the Internet. The Dim statementdeclares the variable readerVar to be of type StreamReader.2. Execute a statement of the formreaderVar = IO.File.OpenText(filespec)where filespec identifies the file to be read. This statement establishes a communi-cationslink between the computer and the disk drive for reading data from the disk.Data then can be input from the specified file and assigned to variables in the pro-gram.This assignment statement is said to open the file for input.Just as with other variables, the declaration and assignment statements in Steps2 and 3 can be combined into the single statementDim readerVar As IO.StreamReader = IO.File.OpenText(filespec)3. Read items of data in order, one at a time, from the file with the ReadLine method.Each datum is retrieved as a string. A statement of the formstrVar = readerVar.ReadLinecauses the program to look in the file for the next unread line of data and assign itto the variable strVar. The data can be assigned to a numeric variable if it is firstconverted to a numeric type with a statement such asnumVar = CDbl(readerVar.ReadLine)Note: If all the data in a file have been read by ReadLine statements and another itemis requested by a ReadLine statement, the item retrieved will have the value Nothing.4. After the desired items have been read from the file, terminate the communicationslink set in Step 3 with the statementreaderVar.Close()*1. Execute a statement of the formDim readerVar As IO.StreamReaderA StreamReader is an object from the Input/Output class that can read a stream ofcharacters coming from a disk or coming over the Internet. The Dim statementdeclares the variable readerVar to be of type StreamReader.2. Execute a statement of the formreaderVar = IO.File.OpenText(filespec)where filespec identifies the file to be read. This statement establishes a communi-cationslink between the computer and the disk drive for reading data from the disk.Data then can be input from the specified file and assigned to variables in the pro-gram.This assignment statement is said to open the file for input.Just as with other variables, the declaration and assignment statements in Steps2 and 3 can be combined into the single statementDim readerVar As IO.StreamReader = IO.File.OpenText(filespec)3. Read items of data in order, one at a time, from the file with the ReadLine method.Each datum is retrieved as a string. A statement of the formstrVar = readerVar.ReadLinecauses the program to look in the file for the next unread line of data and assign itto the variable strVar. The data can be assigned to a numeric variable if it is firstconverted to a numeric type with a statement such asnumVar = CDbl(readerVar.ReadLine)Note: If all the data in a file have been read by ReadLine statements and another itemis requested by a ReadLine statement, the item retrieved will have the value Nothing.4. After the desired items have been read from the file, terminate the communicationslink set in Step 3 with the statementreaderVar.Close()*1. Execute a statement of the formDim readerVar As IO.StreamReaderA StreamReader is an object from the Input/Output class that can read a stream ofcharacters coming from a disk or coming over the Internet. The Dim statementdeclares the variable readerVar to be of type StreamReader.2. Execute a statement of the formreaderVar = IO.File.OpenText(filespec)where filespec identifies the file to be read. This statement establishes a communi-cationslink between the computer and the disk drive for reading data from the disk.Data then can be input from the specified file and assigned to variables in the pro-gram.This assignment statement is said to open the file for input.Just as with other variables, the declaration and assignment statements in Steps2 and 3 can be combined into the single statementDim readerVar As IO.StreamReader = IO.File.OpenText(filespec)3. Read items of data in order, one at a time, from the file with the ReadLine method.Each datum is retrieved as a string. A statement of the formstrVar = readerVar.ReadLinecauses the program to look in the file for the next unread line of data and assign itto the variable strVar. The data can be assigned to a numeric variable if it is firstconverted to a numeric type with a statement such asnumVar = CDbl(readerVar.ReadLine)Note: If all the data in a file have been read by ReadLine statements and another itemis requested by a ReadLine statement, the item retrieved will have the value Nothing.4. After the desired items have been read from the file, terminate the communicationslink set in Step 3 with the statementreaderVar.Close()*1. Execute a statement of the formDim readerVar As IO.StreamReaderA StreamReader is an object from the Input/Output class that can read a stream ofcharacters coming from a disk or coming over the Internet. The Dim statementdeclares the variable readerVar to be of type StreamReader.2. Execute a statement of the formreaderVar = IO.File.OpenText(filespec)where filespec identifies the file to be read. This statement establishes a communi-cationslink between the computer and the disk drive for reading data from the disk.Data then can be input from the specified file and assigned to variables in the pro-gram.This assignment statement is said to open the file for input.Just as with other variables, the declaration and assignment statements in Steps2 and 3 can be combined into the single statementDim readerVar As IO.StreamReader = IO.File.OpenText(filespec)3. Read items of data in order, one at a time, from the file with the ReadLine method.Each datum is retrieved as a string. A statement of the formstrVar = readerVar.ReadLinecauses the program to look in the file for the next unread line of data and assign itto the variable strVar. The data can be assigned to a numeric variable if it is firstconverted to a numeric type with a statement such asnumVar = CDbl(readerVar.ReadLine)Note: If all the data in a file have been read by ReadLine statements and another itemis requested by a ReadLine statement, the item retrieved will have the value Nothing.4. After the desired items have been read from the file, terminate the communicationslink set in Step 3 with the statementreaderVar.Close()**************MsgBox(prompt, , title)is executed, where prompt and title are strings, a message dialog box appears with promptdisplayed and the title bar caption title and stays on the screen until the user pressesEnter, clicks on the box in the upper-right corner, or clicks OK. For instance, the state-mentMsgBox("Nice try, but no cigar.", , "Consolation")******