Search Results

Saturday, April 11, 2009

Parsing Values from String Data

This "Parsing" technique can be extremely helpful when you wish to convert a bit of user input data (such as a selection from a GUI-based drop-down list box) into a numerical value.

Consider the following parsing logic within a method named ParseFromStrings():
//***********************************************************************//
static void ParseFromStrings()
{
Console.WriteLine("=> Data type parsing:");
bool b = bool.Parse("True");
Console.WriteLine("Value of b: {0}", b);
double d = double.Parse("99.884");
Console.WriteLine("Value of d: {0}", d);
int i = int.Parse("8");
Console.WriteLine("Value of i: {0}", i);
char c = Char.Parse("w");
Console.WriteLine("Value of c: {0}", c);
Console.WriteLine();
}
//***********************************************************************//
OutPut:

Value of b:True
Value of d:99.84
Value of i:8
Value of c:w

NOTE:


The important thing to understand in this is Parse function becomes very important when you are working on GUI applications in which you have to take input from user in string format e.g.;

Enter Your Age : 21

Enter Date: 11/04/2009

In these scenarios to convert each data type to its actual type we use the method on Parsing,which is available for every data type(ValueType) present in C#.

No comments:

Post a Comment