473,412 Members | 2,012 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

Cannot implicitly convert type 'double' to 'bool'

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
6 16922
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
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
"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
double expect = int.Parse(txtInput.Text); ???

May 21 '06 #5
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: Christoph Boget | last post by:
I am getting an error (a few among many) for the following lines of code: retval.BrokerName = (( curRow == System.DBNull.Value ) ? SqlString.Null : (string)curRow ); retval.BrokerGroupId = ((...
2
by: Jeff | last post by:
I get the following error: Cannot implicitly convert type 'Factory.Stack.pArrayStack' to 'Factory.Stack.StackDefs.ImStack' at compile time. I thought perhaps there was a mismatch between the...
2
by: Patrick Olurotimi Ige | last post by:
When i convert:- this code from VB to C# Why do i get error "Cannot implicitly convert type 'object' to 'bool' VB --- If cmdcommand.Parameters("ReturnValue").Value = 1 Then lblStatus.Text =...
6
by: eric.goforth | last post by:
What's wrong with this? if (bool)(UserIDs == ThisUserId) { return true; }
2
by: Christophe | last post by:
class A {} class B {} interface MyInterface { void method(A a); void method(B b); }
2
by: Nick | last post by:
I am trying to find out if particular array element contains InputOutput value. private static int FillParameters(DbCommand command, SqlParameter p) { int x = 0; //int initalize to hold zero...
7
by: groups | last post by:
This is my first foray into writing a generic method and maybe I've bitten off more than I can chew. My intent is to have a generic method that accepts a value name and that value will be...
4
by: hellomac23 | last post by:
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
4
by: cowdung7 | last post by:
public bool searchGame(string gameName) { string searchGame = {"Tibia", "Combat Arms", "Need for Speed", "Moto GP", "Risk", "Red Alert", "Monopoly", "Scrabble"}; if ( searchGame ==...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.