0

Trying to learn c# and dont understand why i cant compare string.

if (answere == 'j' || answere == 'J')
            {
                again = true;
            }
            else
                again = false;

How do I do to make this happen in c#?

4
Contributors
9
Replies
10
Views
6 Years
Discussion Span
Last Post by rannamaa
0

If the type of answere is string then you must compare against a string.
'j' and 'J' are of type char. To make them string, use "j" and "J".

0

You cad do it like:

string answare = "j";
bool again = (answare == "j" || answare == "J") ? true : false;

about your issue: as ddanbe explained, (') is a type char, and I presume that variable answare is a type string ("). So these two will no go along. Or use one or the other.

This is a short version where you can choose between true of false.
Mitja

Edited by Mitja Bonca: n/a

0

You cad do it like:

string answare = "j";
bool again = (answare == "j" || answare == "J") ? true : false;

No need for the ? : part, it will return true/false anyway. You can also just do:

bool again = answare.ToUpper() == "J";
0

That one i didnt know before. Youre all wonderfull at this forum!

0

If you would mark this thread as solved, who knows, what wonderfull and miraculous things we can do for you!

0

:) Indeed. Maybe we can find out some thing which even Microsoft doesn`t know, and it will be implemented in VS 4.5
:)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.