1 / 18

Strings and I/O

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

ashanti
Télécharger la présentation

Strings and I/O

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Strings and I/O ISAT 252:Analytical Methods IVVB.NET

  2. Assignment • Should have read Chapter 3 • pp. 231-241

  3. 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

  4. 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)

  5. 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.

  6. Problem – compute area of circle • Allow user to enter radius • Pseudocode: • Declare variables • Get input and convert • Compute area • Display output

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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()

  12. 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

  13. Format Specifier Codes

  14. Format Specifier Code Examples

  15. 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

  16. 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()

  17. 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

  18. 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

More Related