Cannot implicitly convert type 'int' to 'bool'

Possible duplicate:
Convert type help - cannot implicitly convert type 'string to' bool

I am very new to the language. I am not a very good programmer. This code gives me an error:

cannot implicitly convert int to bool.

I'm not sure what I'm doing wrong. Can someone tell me what I'm doing wrong. Any help would be appreciated and any recommendation would also help.

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication2 { class mysteryVal { public const int limitOfGuess = 5; // Data member public int mystVal; private int numOfGuess ; private randomNumberMagnifier mag = new randomNumberMagnifier(); public int randomMag(int num) { return num + mystVal; } // Instance Constructor public mysteryVal() { mystVal = 0; numOfGuess = 0; } public void game(int user) { int userInput = user; if (numOfGuess < limitOfGuess) { numOfGuess++; if (userInput = mag.randomMagnifier()) { } } } } } 
+6
source share
5 answers

Line

 if (userInput = mag.randomMagnifier()) 

it should be

 if (userInput == mag.randomMagnifier()) 
+9
source

To fix this:

 if (userInput = mag.randomMagnifier()) 

in

 if (userInput == mag.randomMagnifier()) 

Here you assign a value in the if , which is not true. You must check the condition, to check the condition u you need to use "==" .
The if returns boolean values ​​and because you assign a value here, it gives an error.

+11
source

you should use == instead of = change: Lif(userinput = mag.randommagnifier()) for

 if(userinput == mag.randommagnifier()) 
+3
source

The if statement always contains an expression that evaluates to a boolean value. Your line

 if (userInput = mag.randomMagnifier()) 

is not a bool , which causes an error. You probably meant

 if (userInput == mag.randomMagnifier()) 
+3
source

Condition

 userInput = mag.randomMagnifier() 

should be

 userInput == mag.randomMagnifier() 

That you are trying to assign a value to userInput, and then trying to convert int to bool. With C #, this is not possible.

+3
source

Source: https://habr.com/ru/post/927522/


All Articles