4

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.

1
  • You could do something else entirely: List<String> validDays = new List<string> { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" }; if (!validDays.Contains(vDay)) Commented May 8, 2014 at 10:54

6 Answers 6

8

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");
}
Sign up to request clarification or add additional context in comments.

1 Comment

This is ok. But it doesn't answer the Op's original problem of why the error says "cannot be applied to bool and string".
6

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

5 Comments

or check with List.Contains
Ah so for each one. i get it now, thank you very much :)
@al-Acme: You may want to explain a bit to the Op, why the error says "cannot be applied to bool and string"
Need to use && not || here to keep the logic.
@AnttiHaapala Indeed, otherwise it will be always true.
5

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.

Comments

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

Comments

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

Comments

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

1 Comment

Can you turn this into something of a helpful answer? It is unreadable, code-only (at most) and afaict totally unrelated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.