need Help with Very simple code :(

T

Tony

int num;
num=1;
if (num=1)
{
MessageBox.Show("Num=1","num_message");
}
num=3;
if (num < 4)
{
MessageBox.Show("num is less than 4","nummy");
}


Trying the above code errors out on if (num=1) because
"Cannot implicitly convert type 'int' to 'bool'"

How would I modify this to make it work??

thanks :)

Tony!
 
G

Greg Ewing [MVP]

Tony, add an =. C# uses a double == for comparisons:

if (num==1)
{
MessageBox.Show("Num=1","num_message");
}

Your code was actually assigning the value 1 to num and then the if was
trying to decide whether 1 was true or false.
 
T

Tony

Tony, add an =. C# uses a double == for comparisons:

if (num==1)
{
MessageBox.Show("Num=1","num_message");
}

Your code was actually assigning the value 1 to num and then the if was
trying to decide whether 1 was true or false.

DOH..

Old coding habits are hard to break :)

Thanks very much for the reply :)

Tony!
 
G

Guest

Hello

use this instea
if(num == 1)

..


this should work for what you are trying to do which is to check to see if the int variable 'num' is equal to 1. Using only one '=' sing means that you are assinging what is on the right hand side to the variable on the left hand side.
 
S

Slaven

Tony said:
int num;
num=1;
if (num=1)
{
MessageBox.Show("Num=1","num_message");
}
num=3;
if (num < 4)
{
MessageBox.Show("num is less than 4","nummy");
}


Trying the above code errors out on if (num=1) because
"Cannot implicitly convert type 'int' to 'bool'"

How would I modify this to make it work??

thanks :)

Tony!

int num;
num=1;
if (num==1) // <-------
{
....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top