hexce
hexce

Reputation: 161

Operator || cannot be applied to type bool and string / string and string

Simple task that I am finding really difficult.

 Console.Write("[" + CurrentTime + "] Name a day of the week? ");
 string vDay = Console.ReadLine();
 if (vDay != "Monday" || "Tuesday" || "Wednesday" || "Thursday" || "Friday")
 {
  Console.WriteLine("that is not a valid day of the week");
 }

Firsty when I use != it gives me an error saying "cannot be applied to bool and string" without the != and just the = I get "string and string"

Basically what I am trying to do is if someone types "hello" for example it will say that is not a valid day of the week.

Such a simple task but im finding it so difficult, thanks for any help.

Upvotes: 4

Views: 19851

Answers (6)

arup
arup

Reputation: 1

Text='<%# Eval("STS").ToString() == "N" ||Eval("STS").ToString() =="R"  ? "<h5>Approve?</h5>" : "<h5>Approved!</h5>"  %>' 

Upvotes: -2

John Rams
John Rams

Reputation: 13

if (vDay == "Monday" ||vDay == "Tuesday" ||vDay == "Wednesday" ||vDay == "Thursday" ||vDay == "Friday")
 {
  Console.WriteLine("is not a valid day of the week");
 }

This should do it.

Upvotes: 0

Abhitalks
Abhitalks

Reputation: 28397

Since all the answers here do not explain why you are facing that problem in the first place, I shall attempt to do so.

when I use != it gives me an error saying "cannot be applied to bool and string"

This is because, in your code:

...vDay != "Monday" || "Tuesday" ...

vDay is compared to string "Monday" which evaluates successfully and then the result (which is boolean) is compared to string "Tuesday". This gives you the problem of "...cannot be applied to bool and string.."

without the != and just the = I get "string and string

vDay is being assigned the value of "Monday" (which could result in string "Monday") however the string "Monday" is being compared with string "Tuesday". This gives you the problem of "...cannot be applied to string and string..".

The correct way would be to specify operators separately:

...vDay != "Monday" && vDay != "Tuesday"...

Or using the other ways as best described by other answers.

Upvotes: 5

StuartLC
StuartLC

Reputation: 107277

Here's another take, using set-based logic.

 var days = new HashSet<string> { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" };
 if (!days.Contains(vDay))
 {
    Console.WriteLine("that is not a valid day of the week");
 }

Edit

I guess to explain the error:

Operator != on String accepts only a single string on the RHS of the operator, and neither does C# support List creation through the logical or || operator (if this is what was intended, nor conversely, projection of the != operator across the ||ed strings). However, a set can be created and set operations such as Contains can be used. Hashset would typically be my first choice in such a scenario, as it acts as an indexed lookup (although arguably overkill for a set of ~5 strings, this will scale much better than List or Array for much larger sets).

Upvotes: 4

Sadique
Sadique

Reputation: 22821

This is what you need:

if (vDay != "Monday" && vDay != "Tuesday" && vDay != "Wednesday" && vDay != "Thursday" && vDay != "Friday")
{
      Console.WriteLine("that is not a valid day of the week");
}

Upvotes: 6

Science_Fiction
Science_Fiction

Reputation: 3433

Might be cleaner to have something like:

List<string> list = new List<string> { "Monday", "Tuesday", "Wednesday", "Thursday", ... };

if (!list.Contains(vDay ))
{
    Console.WriteLine("that is not a valid day of the week");
}

Upvotes: 8

Related Questions