Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture Set 6 The String and DateTime Data Types

Similar presentations


Presentation on theme: "Lecture Set 6 The String and DateTime Data Types"— Presentation transcript:

1 Lecture Set 6 The String and DateTime Data Types
Part C – String and Data Validation The StringBuilder class

2 Objectives Get an introduction to concept of data validation
Gaining greater flexibility in manipulating strings with strings of the StringBuilder type 11/29/ :55 AM

3 Validating User Entries
We will later learn a lot more about user input validation when we cover the material on handling exceptions and input validation For now we provide a short introduction to the concept of input validation There are lots of ways in which users provide input through a form but one important one is through the use of a textbox User enters textual information (a string) This string is most often then converted to some internal form such as a number or date 11/29/ :55 AM

4 Converting Textbox String Input
We have several ways to convert a string to a value The Parse common method Decimal.Parse(salesString) Casting operators such as (int),(double) etc. (DateTime)txtDueDate.Text Methods associated with each type, such as ToDouble, ToInt32, and ToString System.Convert.ToDouble(myAge) The question is – when you use one of these approaches, how do you know whether the input string is properly structured for the requested conversion? IsDecimal, IsDate, IsNumeric exist in VB but in C# you have to write your own 11/29/ :55 AM

5 StringBuilder Class Used to create mutable strings as opposed to the immutable ones we have seen so far. For immutable strings, when the value (and usually the length) is changed, the old string is deleted and a new one of the correct length is created. Always use this class when you are working with strings and doing inserts, replacements, appends, and removals. Why?  11/29/ :55 AM

6 StringBuilder Class – how it works
The StringBuilder type always retains the original memory initially allocated to a string. If you do not specify the capacity of a string when you declare it as of type StringBuilder, you get a string of capacity 16 If you assign a string data value having a length that is larger than a string variable’s capacity, StringBuilder adds to the capacity of the string variable – it doubles its capacity StringBuilder is just another data type You need using System.Text. 11/29/ :55 AM

7 StringBuilder Functions (read all about them)
The String and StringBuilder classes provide all the methods you need in working with strings (you can look them up – just use the correct notations in calling them) Creating a StringBuilder object with 0, 1, or 2 arguments – (initial value and / or length) StringBuilder phone = new StringBuilder(“ ”, 10); 11/29/ :55 AM


Download ppt "Lecture Set 6 The String and DateTime Data Types"

Similar presentations


Ads by Google