-
Case with && Error!
hi,
Im getting an error on the case lines in Red:
========================================
Cannot implicitly convert type 'bool' to 'int'
========================================
Any help please. Thanks
Code:
switch (total)
{ case (total < 50):
grade = "FAIL";
case (total >= 50 && total < 70):
grade = "PASS";
case (total > 70 && total < 80):
grade = "MERIT";
case (total >= 80 && total < 90):
grade = "DISTINCTION";
-
Re: Case with && Error!
Hello,
I do not believe that this is possible in the C# Specification.
http://msdn.microsoft.com/en-us/libr...7t(VS.71).aspx
I believe you will have to do this using If's.
Hope this helps!!
Gary
-
Re: Case with && Error!
Still get the same error even with an if ... else if statements. So it seems something is wrong in the logical expression.
-
Re: Case with && Error!
Hey,
I certainly wouldn't call it weak.
This seems to be possible in VB:
http://www.java2s.com/Tutorial/VB/00...tegervalue.htm
But not so in C#. I am sure there is a very good reason to this, but I don't know what that reason is.
Gary
-
Re: Case with && Error!
Try an additional set of parenthesis:
Code:
((total >= 50) && (total < 70)):
Make sure you know exactly what order things are processing in.
-tg
-
Re: Case with && Error!
Hey,
Techgnome, I don't think that is going to help.
When I attempted to convert the code in the link that i posted I got the following error:
"Case labels with binary operators are unsupported"
Therefore, I simply think that this case statement functionality is not supported in the C# language specification.
Gary
-
Re: Case with && Error!
Try turning on Option Strict in VB, then even it will expect Constant expressions in CASE and Ranged integral values will throw error.
@OP, you are pretty much out of luck here. You have to use IF-THEN-ELSEIF conditions in your case.