In writing program, data type conversion is very common, especially when you have the user input value in string. This string value may represent the equivalent value of a number (”12″), char (”a”), bool (”true“), or a color enumeration (”Red”). You need to convert this string to its equivalent value type. Therefore, the number “12″ would be converted to a numeric value such as int, short, float, etc. The string “a” would be converted to a char value ‘a’, the string “true” would be converted to a bool value, and the color “Red” could be converted to an enumeration value (if an enumeration were defined that contained the element Red)
The Parse method of the type can be used to convert the string. The following code converts a string containing a number to its numeric type:
using System; using System.Globalization; ... // This code requires the use of the System and System.Globalization namespaces string longString = "7654321"; int actualInt = Int32.Parse(longString); // longString = 7654321 string dblString = "-7654.321"; double actualDbl = Double.Parse(dblString, NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign); // longString = "-7654.321
To convert a string containing a Boolean value to a Boolean type, use the following code:
using System; ... // This code requires the use of the System namespace string boolString = "true"; bool actualBool = Boolean.Parse(boolString); // actualBool = true
To convert a string containing a char value to a char type, use the following code:
using System; ... // This code requires the use of the System namespace string charString = "t"; char actualChar = char.Parse(charString); // actualChar = ''t''
To convert a string containing an enumeration value to an enumeration type, use the following code:
using System; ... // This code requires the use of the System namespace enum Colors { red, green, blue } string colorString = "blue"; // Note that the Parse method below is a method defined by System.Enum, not by Colors Colors actualEnum = (Colors)Colors.Parse(typeof(Colors), colorString); // actualEnum = blue
The static Parse method on certain types derived from the ValueType data types allows easy conversion from a string value to the value of that specific value type. The Parse method is supported by the following types: Boolean (bool), Byte (byte), Decimal (decimal), Double (double), Int16 (short), Int32 (int), Int64 (long), SByte (sbyte), Single (float), UInt16 (ushort), UInt32 (uint), UInt64 (ulong).
In addition to the Parse methods that take a single string parameter and convert it to the target data type, each numeric type has a second overloaded version of the Parse method that includes a second parameter of type System.Globalization.NumberStyles. This allows the Parse method to correctly handle specific properties of numbers, such as leading or trailing signs, decimal points, currency symbols, thousands separators, etc. NumberStyles is marked as a flag-style enumeration, so you can bitwise OR more than one enumerated value together to allow a group of styles to be used on the string.
The NumberStyles
|
If the NumberStyle parameter is not supplied when it is required (as when, for example, a numeric string includes a thousands separator), or if the NumberStyle enumeration is used on a string that does not contain a number in the supplied NumberStyle format, a FormatException exception will be thrown. If the size of the number in the string is too large or too small for the data type, an OverFlowException exception will be thrown. Passing in a null for the SourceString parameter will throw an ArgumentNullException exception.
The Parse method of the two non-numeric data types, bool and char, also deserve some additional explanation. When calling Boolean.Parse, if a string value contains anything except a value equal to the static properties Boolean.FalseString, Boolean.TrueString, or the string literals “false” or “true” (which are case-insensitive), a FormatException exception is thrown. Passing in a null for the SourceString parameter throws an ArgumentNullException exception.
When invoking char.Parse, if a string value containing more than one character is passed as its single argument, a FormatException exception is thrown. Passing in a null for the string parameter throws an ArgumentNullException exception.
The static Enum.Parse method returns an Object of the same type as specified in the first parameter of this method (EnumType). This value is viewed as an Object type and must be cast to its correct enumeration type. This method throws an ArgumentException exception if the Value parameter cannot be matched to a string in the enumeration. An ArgumentNullException exception is thrown if a null is passed in to the Value parameter.
Popularity: 1% [?]
RSS feed for comments on this post · TrackBack URI
Leave a reply