Hi im trying to write a if...else for C# but i keep getting Cannot implicitly convert type 'int' to 'bool' i really lost can anybody help me on what that means
Cannot implicitly convert type 'int' to 'bool' ?????
Collapse
X
-
Tags: None
-
You are probably using if statements or a loop, in which you try and use an integer to determine the truth value. For instance:
C and C++ allow this, maybe C# doesn't?Code:int myFunction(int a, int b) { if (a < b) return -1; if (a == b) return 0; if (a > b) return 1; } // ...later in your code... if (myFunction(3, 5)) { // ... } // OR while (myFunction(x, 3)) { // ... } -
You're on the right track. In C++ a zero is false, and anything not zero is true.Originally posted by Ganon11You are probably using if statements or a loop, in which you try and use an integer to determine the truth value. For instance:
C and C++ allow this, maybe C# doesn't?Code:int myFunction(int a, int b) { if (a < b) return -1; if (a == b) return 0; if (a > b) return 1; } // ...later in your code... if (myFunction(3, 5)) { // ... } // OR while (myFunction(x, 3)) { // ... }
C# doesn't work that way. You need to actually have boolean responses of true and false.
really says " if (value returned from myFunction(3,5) is true) { ... }"Code:if (myFunction(3, 5)) {
But your method return an int, not a bool. So the error message complaining "I don't know how to convert your returned int into a bool." makes sense.
You need a boolean as the final evaluation of your if (....) statement.
Try something like this if myFunction will continue to return an int
Otherwise, you can change myFunction to return a bool depending on the conditions.Code:if (myFunction(3,5) != 0) { // Then do this }Comment
Comment