Answered by:
Error - Operator '==' cannot be applied to operands of type string and char

Question
-
User-1499457942 posted
Hi
On below line i get above error
if (dt.Rows[0]["Eligible"].ToString() == 'Y')
{
}Thanks
Sunday, September 9, 2018 5:03 PM
Answers
-
User753101303 posted
Hi,
Use "Y" to get a string. A literal within single quotes is a character.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Sunday, September 9, 2018 5:59 PM -
User409696431 posted
Singe quotes in that context imply char rather than string. It means you are encoding a single character into data type char. For a string, use double quotes.
Try
if (dt.Rows[0]["Eligible"].ToString() == "Y")
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Sunday, September 9, 2018 6:04 PM -
User-1716253493 posted
C# deferent than VB that has auto convertion feature.
Ie, In VB you can compare a text with a number.
But in C#, if you compare two object you must ensure that both object have same type.
Comparing chars
if(Convert.ToChar(dt.Rows[0]["Eligible"])=='y') { }
Comparing strings
if (dt.Rows[0]["Eligible"].ToString() == "y") { }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, September 10, 2018 12:49 AM
All replies
-
User753101303 posted
Hi,
Use "Y" to get a string. A literal within single quotes is a character.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Sunday, September 9, 2018 5:59 PM -
User409696431 posted
Singe quotes in that context imply char rather than string. It means you are encoding a single character into data type char. For a string, use double quotes.
Try
if (dt.Rows[0]["Eligible"].ToString() == "Y")
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Sunday, September 9, 2018 6:04 PM -
User-1716253493 posted
C# deferent than VB that has auto convertion feature.
Ie, In VB you can compare a text with a number.
But in C#, if you compare two object you must ensure that both object have same type.
Comparing chars
if(Convert.ToChar(dt.Rows[0]["Eligible"])=='y') { }
Comparing strings
if (dt.Rows[0]["Eligible"].ToString() == "y") { }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, September 10, 2018 12:49 AM