Read int and bool

  • Thread starter Thread starter gh
  • Start date Start date
G

gh

Hi every Body ..

in C# if I wont the user to Input a Number I write the
code:

// ... the 1st way for int

String Number;
int X;
Console.Write("Enter A Number:");
Number = Console.ReadLine();
X = Convert.ToInt32(Number);


..
..
and if i wont to Enter a Boolian Value I will write the
code

String BOOL;
char CBOOL;
bool x;

Console.Write("Enter a Bool Value t/f");
BOOL = Console.readLine();
CBOOL = Convert.ToChar(BOOL); // from string to char
if ((CBOOL == 't')||(CBOOL == 'T')) // if the input if
true
{x = true ;}
else if ((CBOOL == 'f')||(CBOOL == 'F')) // if the input
if false
{x = false;}
else{Console.Write("Error ... Not Valid Input");} // if
nither True nor False

..
...
SO , ... is there a method so it Read the Interger or
bool directly with out the use of string and then the
conversion ..?
..
 
Hi,

for reading int you can use this:
int i = int.Parse(Console.ReadLine());

for bool:
bool b = bool.Parse(Console.ReadLine());


--
Mit freundlichen Grüßen -- Regards

Ralph Gerbig
www.ralphgerbig.de.vu
(e-mail address removed)
 
thank you Ralph Gerbig....

-----Original Message-----
Hi,

for reading int you can use this:
int i = int.Parse(Console.ReadLine());

for bool:
bool b = bool.Parse(Console.ReadLine());


--
Mit freundlichen Grüßen -- Regards

Ralph Gerbig
www.ralphgerbig.de.vu
(e-mail address removed)



.
 
Back
Top