+ 1

Example: Console.WriteLine("value of a="); Char a= Console.ReadLine(); if(b>2 && c>3 || a>2) ...... Else ...... It gives error like... "cannot implicitly convert type 'string' to 'char'..." So now please tell me how to convert it... And how to use in code

17th Jun 2016, 7:04 PM
Ajay Mangaj
4 Answers
+ 4
use this: char a = Convert.ToChar(Console.ReadLine()); Hope that'll solve the problem.
17th Jun 2016, 7:10 PM
erwinmesi
erwinmesi - avatar
+ 1
To mitigate against the potentiality of somebody typing in more than 1 character and breaking the program, try an if statement, and then convert to a char, i.e.: Console.WriteLine("Value of a = "); string aCheck = Console.ReadLine(); if(aCheck.Length > 1){ Console.WriteLine("Only 1 char please."); } else{ char a = aCheck[0]; //or you can use the "Convert.ToChar" method here, as mesiaserwin showed. if(b>2 && c>3 || a>2){ //... } }
17th Jun 2016, 8:28 PM
Malachi Jones
Malachi Jones - avatar
0
Yes..Thanx.. The problem is solved
17th Jun 2016, 7:18 PM
Ajay Mangaj
0
I'm glad to be of help :D
17th Jun 2016, 8:31 PM
erwinmesi
erwinmesi - avatar