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

14,310

Solution 1

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.

Solution 2

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");
}

Solution 3

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");
}

Solution 4

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).

Share:
14,310
hexce
Author by

hexce

Updated on June 27, 2022

Comments

  • hexce
    hexce almost 2 years

    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.