180 likes | 323 Views
Strings and I/O. ISAT 252:Analytical Methods IV VB.NET. Assignment. Should have read Chapter 3 pp. 231-241. String Properties and Methods. See pp. 231-241 What does each of these do? str.Length str.Substring(m, n) str.IndexOf(str2) str.ToUpper str.ToLower str.Trim
E N D
Strings and I/O ISAT 252:Analytical Methods IVVB.NET
Assignment • Should have read Chapter 3 • pp. 231-241
String Properties and Methods • See pp. 231-241 What does each of these do? • str.Length • str.Substring(m, n) • str.IndexOf(str2) • str.ToUpper • str.ToLower • str.Trim • Remember, string indexes start at 0
Conversion of Data types • To convert values in a data type to the equivalent value in another data type • Converts non-numeric data (strings|text values) to numeric data • Double.Parse() or Integer.Parse() method - Use to convert input text in a text box/string variable into a number TotalItemsInteger =Integer.parse("123") SalesPriceDouble = Double.Parse(txtPrice.Text) TotalScoreDouble = TotalScoreDouble + _ Double.Parse(txtScore.Text)
Conversion • Converts a non-string value in in its equivalent string of characters • ToStringMethod • Example* lblOutput.text = (1 + 2 – 3).ToString lblOutput.text = intItems.ToString lblOutput.text = (dblTotal*(1 + dblTax)).ToString • *note I am using the shorter naming convention here for a label control. Our book uses OutputLabel.Text = (1 + 2 – 3).ToString, etc.
Problem – compute area of circle • Allow user to enter radius • Pseudocode: • Declare variables • Get input and convert • Compute area • Display output
Private SubbtnCompute_Click(..) handles _ btnCompute.Click ' compute the area of a circle ConstdblPIas Double = 3.14 DimdblRadiusAs Double Dim dblCircleAreaAs Double ‘Convert the input radius into a number dblRadius = CDbl(txtRadius.Text) 'compute the area of the circle dblCircleArea= dblPI * dblRadius ^ 2 'output the area of the circle txtOut.Text = "The area of the circle is " + _ dblCircleArea.ToString & " inches“ End Sub Data declaration Calculation Output Input and convert the appropriate values Example: compute the area of a circle
Displaying Text Output • Labels • Multiple lines, but it will not allow the user to scroll when all the lines are not visible • Use CStr or To.String to convert numbers to string • List Boxes • Use lstName.Items.Add( ) for each line • Use CStr or To.String to convert numbers to string • Message Boxes • See p. 140
Displaying Text Output • Textbox • When ReadOnly Property set to TRUE, a textbox is strictly an output control • You can display multiple lines in a textbox by setting the property Multiline to TRUE • You can allow the user to scroll horizontally and vertically by setting the appropriate scrollbars in the property Scrollbars • Use CStr or To.String to convert numbers to string
Formatted Output • Can adjust spacing by inserting blanks • blanks should be within the quote marks • works best with a fixed size font such as Courier • Numeric Output • can use pre-defined formats such as “FormatCurrency” and “FormatPercent” • can create custom columnar formats using zones
Formatting Data for Display • To display numeric data in a label or text box, first convert value to string. • Use ToString method • Format the data using formatting codes. • Specifies use of dollar sign, percent sign, and commas • Specifies number of digits that appear to right of decimal point DisplayTextBox.Text = NumberInteger.ToString()
Using Format Specifier Codes • "C" code • Currency — String formatted with dollar sign, commas separating each group of 3 digits and 2 digits to the right of decimal point • "N" code • Number — String formatted with commas separating each group of 3 digits and 2 digits to the right of decimal point • Can specify number of decimal positions • Example: "C0" zero digits
Getting Input • From a Textbox • Always a string • Use Double.Parse or Integer.Parse if you are putting values into number variable OR • You can use CDbl or CInt if you are putting values into number variables • From an Input Dialog Box • See Textbook
Getting Input • From Files see p. 541-546 • Declare the file Dim readerVar As IO.StreamReader • Open the file readerVar = IO.File.OpenText (filespec) • Read from file strVar = readerVar.ReadLine numVar = Double.Parse (readerVar.ReadLine) ( OR numVar = CDbl (readerVar.ReadLine) ) • Close the file readerVar.Close()
Example of Reading from Files Private Sub ReadFileButton_Click() Handles ReadFileButton.Click Dim strName1 As String Dim strName2As String Dim fileNames As IO.StreamReader fileNames = IO.File.OpenText("Names.txt") ‘Names.txt is in the Debug folder of the bin for the project strName1 = fileNames.ReadLine strName2 = fileNames.ReadLine fileNames.Close() txtOutput.Text = strName1+ " and " + strName2 End Sub
Assignments Do Lab 6 and Lab 7 • Answer the Question Sheet • Next Lecture • Read Chapter 4 (pages 205 – 252) • Do Tutorials • 4-2 • 4-4