By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,465 Members | 1,463 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,465 IT Pros & Developers. It's quick & easy.

Cannot implicitly convert type 'double' to 'bool'

P: n/a
Hi

I have a short piece of trial code that compares some input and then
produces a message based on the value.

However I get a build error that i dont know how to resolve ' Cannot
implicitly convert type 'double' to 'bool'

The code is

double expect = int.Parse(txtInput.Text);
if (expect <100) MessageBox.Show("This is less than 100");

else if (expect = 100) MessageBox.Show("This is equal to 100");

else MessageBox.Show("This is greater than 100","Result");

}

}

The error relates to the 'else if' line.

Please advise what the error means and how to resolve it.

Thanks

Doug


May 21 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
HI Doug,

The problem is you're using an assignment operator (=) instead of an
equality comparison operator (==) in:

if (expect = 100) // should be '=='

Regards,

Octavio

"Doug" <qu********@hotmail.com> escribió en el mensaje
news:44***********************@news.optusnet.com.a u...
Hi

I have a short piece of trial code that compares some input and then
produces a message based on the value.

However I get a build error that i dont know how to resolve ' Cannot
implicitly convert type 'double' to 'bool'

The code is

double expect = int.Parse(txtInput.Text);
if (expect <100) MessageBox.Show("This is less than 100");

else if (expect = 100) MessageBox.Show("This is equal to 100");

else MessageBox.Show("This is greater than 100","Result");

}

}

The error relates to the 'else if' line.

Please advise what the error means and how to resolve it.

Thanks

Doug

May 21 '06 #2

P: n/a
Thanks - oops - sorry I am just coming back to this stuff - too long away!!!
"Octavio Hernandez" <oc*****************@gmail.com> wrote in message
news:OF**************@TK2MSFTNGP03.phx.gbl...
HI Doug,

The problem is you're using an assignment operator (=) instead of an
equality comparison operator (==) in:

if (expect = 100) // should be '=='

Regards,

Octavio

"Doug" <qu********@hotmail.com> escribió en el mensaje
news:44***********************@news.optusnet.com.a u...
Hi

I have a short piece of trial code that compares some input and then
produces a message based on the value.

However I get a build error that i dont know how to resolve ' Cannot
implicitly convert type 'double' to 'bool'

The code is

double expect = int.Parse(txtInput.Text);
if (expect <100) MessageBox.Show("This is less than 100");

else if (expect = 100) MessageBox.Show("This is equal to 100");

else MessageBox.Show("This is greater than 100","Result");

}

}

The error relates to the 'else if' line.

Please advise what the error means and how to resolve it.

Thanks

Doug


May 21 '06 #3

P: n/a
"Doug" <qu********@hotmail.com> wrote:
double expect = int.Parse(txtInput.Text);
...
else if (expect = 100) MessageBox.Show("This is equal to 100");


That looks very dodgy. You should never compare whether one floating
point number is equal to another (unless one of them is 0 or 1).

--
Lucian
May 21 '06 #4

P: n/a
double expect = int.Parse(txtInput.Text); ???

May 21 '06 #5

P: n/a
Hi,

Thanks - but can you expand on the reason for this?

Doug
"Lucian Wischik" <lu***@wischik.com> wrote in message
news:16********************************@4ax.com...
"Doug" <qu********@hotmail.com> wrote:
double expect = int.Parse(txtInput.Text);
...
else if (expect = 100) MessageBox.Show("This is equal to 100");


That looks very dodgy. You should never compare whether one floating
point number is equal to another (unless one of them is 0 or 1).

--
Lucian

May 22 '06 #6

P: n/a
The reason is simply that a floating point number often contains a
number that is approximately what you stored in it. If you do:

double x = 3;

The variable x probably contains something like 2.99999999999999999 or
3.000000000000001 rather than the exact value 3. If you now compare this
"almost 3" to the value of some other variable that is "almost 3", the
result is rather unpredictable.

Doug wrote:
Hi,

Thanks - but can you expand on the reason for this?

Doug
"Lucian Wischik" <lu***@wischik.com> wrote in message
news:16********************************@4ax.com...
"Doug" <qu********@hotmail.com> wrote:
double expect = int.Parse(txtInput.Text);
...
else if (expect = 100) MessageBox.Show("This is equal to 100");

That looks very dodgy. You should never compare whether one floating
point number is equal to another (unless one of them is 0 or 1).

--
Lucian


May 22 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.