7

Possible Duplicate:
Help converting type - cannot implicitly convert type ‘string’ to ‘bool’

I am very new to the language n I am not a good programmer. This code is giving me error:

cannot implicitly convert type int to bool.

I am not sure what I am doing wrong. Can some tell me what I am doing wrong. Any help would be appreciated n any recomendation 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())
                   {
                }
               }

           } 


           }
                } 
Community
  • 1
  • 1
user1730332
  • 85
  • 1
  • 1
  • 6

5 Answers5

13

Correct this:

if (userInput = mag.randomMagnifier())

to:

if (userInput == mag.randomMagnifier())

Here you are assigning the value in the if statement, which is wrong. You have to check the condition, for checking condition u have to use "==".
if statement returns boolean values, and because you are assigning value here, it's giving the error.

SimpleVar
  • 14,044
  • 4
  • 38
  • 60
Ravindra Bagale
  • 17,226
  • 9
  • 43
  • 70
11

The line

if (userInput = mag.randomMagnifier())

should be

if (userInput == mag.randomMagnifier())
Jacob Mattison
  • 50,258
  • 9
  • 107
  • 126
3

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

if(userinput == mag.randommagnifier())
ESD
  • 675
  • 2
  • 12
  • 35
3

An if statement always contains an expression which evaluates to a boolean value. Your line

if (userInput = mag.randomMagnifier())

is not a bool which is what is causing the error. You probably meant

if (userInput == mag.randomMagnifier())
Tejas Sharma
  • 3,420
  • 22
  • 35
3

The condition

userInput = mag.randomMagnifier() 

needs to be

userInput == mag.randomMagnifier()

What you have is trying to assign the userInput value and then it tries to convert the int to bool. With C# this is not possible.