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 | SoloLearn: Learn to code for FREE!

+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

6/17/2016 7:04:31 PM

Ajay Mangaj

4 Answers

New Answer

+4

use this: char a = Convert.ToChar(Console.ReadLine()); Hope that'll solve the problem.

+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){ //... } }

0

Yes..Thanx.. The problem is solved

0

I'm glad to be of help :D