Monday, July 29, 2013

The Convert Class

What's your name?
            string name;
     Console.WriteLine("What is your name");
     name = Console.ReadLine();// This will create the user input at enter
     Console.WriteLine("Hello " + name);
     Console.Read();

What's your number- ..hold up I gotta convert the string
            int celly;
     int homePhone;

     Console.WriteLine("What is your cell number?");
     celly = Convert.ToInt32(Console.ReadLine());// the readline is for text input, so any integers need to be converted.

     Console.WriteLine("What is your home number?");
     homePhone = Convert.ToInt32(Console.ReadLine());

     Console.WriteLine("Your cell number is " + celly + " and your home number is " + homePhone);
     Console.WriteLine("These numbers added together is " + (celly + homePhone));
// note "+ (celly + homePhone)" in parentheses  If this were done without parentheses, this would simply concatenate the variables to the writeline string instead of performing the math operation.
     Console.Read();

Double
     double myDoubleVar;
     myDoubleVar Convert.ToDouble(Console.ReadLine());

Floats
     double myFloatVar;
     myFloatVar (float)Convert.ToDouble(Console.ReadLine());

No comments:

Post a Comment