I have an if
statement as follows
if (1 <= value <= 20)
{
}
value
is a double
.
however I get an error which says that "Operator '<=' cannot be applied to operands of type 'bool' and 'double'"
Is there a way around this error?
I have an if
statement as follows
if (1 <= value <= 20)
{
}
value
is a double
.
however I get an error which says that "Operator '<=' cannot be applied to operands of type 'bool' and 'double'"
Is there a way around this error?
Problem : you can not check two expressions without Combining them.
Solution : i think you want to check whether value
is in beteween 1 and 20
You can Use Logical AND &&
operator for checking this
Try This:
if (value >= 1 && value <= 20)
{
}
1 <= value evaluates out to be bool
(false if value is less than 1 and true otherwise).
So it evaluates out to be (true <= 20) or (false <=20)
and error states clearly that you cannot use operator <=
to compare bool and double.
You need and (&&) operator to do comparison:
if (1 <= value && value <= 20)
{
}
You:
if (1 <= value <= 20)
{
}
That is interpreted as
(1 <= value) <= 20
so first 1 will be compared with value
and it will be determined if the former is "less than or equal" the latter. That gives a boolean. Then that boolean, True or False, is compared with <=
to 20
. But you cannot ask if True/False is less than or equal 20
, in C#.
C# doesn't allow you to do this.
Do like this:
The problem is not the
double
, problem is your syntax is incorrect.