473,765 Members | 2,059 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Cannot implicitly convert type 'int' to 'bool' ?????

1 New Member
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
Oct 18 '08 #1
4 9929
Ganon11
3,652 Recognized Expert Specialist
You are probably using if statements or a loop, in which you try and use an integer to determine the truth value. For instance:

Expand|Select|Wrap|Line Numbers
  1. int myFunction(int a, int b) {
  2.    if (a < b) return -1;
  3.    if (a == b) return 0;
  4.    if (a > b) return 1;
  5. }
  6.  
  7. // ...later in your code...
  8.  
  9. if (myFunction(3, 5)) {
  10.    // ...
  11. }
  12.  
  13. // OR
  14.  
  15. while (myFunction(x, 3)) {
  16.    // ...
  17. }
C and C++ allow this, maybe C# doesn't?
Oct 18 '08 #2
JosAH
11,448 Recognized Expert MVP
I'm moving this thread over to the .NET forum where the C# questions are posted.

kind regards,

Jos (moderator)
Oct 18 '08 #3
SvenV
50 New Member
Please post the piece of code where the exceptiong is thrown
Oct 18 '08 #4
tlhintoq
3,525 Recognized Expert Specialist
You are probably using if statements or a loop, in which you try and use an integer to determine the truth value. For instance:

Expand|Select|Wrap|Line Numbers
  1. int myFunction(int a, int b) {
  2.    if (a < b) return -1;
  3.    if (a == b) return 0;
  4.    if (a > b) return 1;
  5. }
  6.  
  7. // ...later in your code...
  8.  
  9. if (myFunction(3, 5)) {
  10.    // ...
  11. }
  12.  
  13. // OR
  14.  
  15. while (myFunction(x, 3)) {
  16.    // ...
  17. }
C and C++ allow this, maybe C# doesn't?
You're on the right track. In C++ a zero is false, and anything not zero is true.
C# doesn't work that way. You need to actually have boolean responses of true and false.

Expand|Select|Wrap|Line Numbers
  1. if (myFunction(3, 5)) {
really says " if (value returned from myFunction(3,5) is true) { ... }"
But your method return an int, not a bool. So the error message complaining "I don't know how to convert your returned int into a bool." makes sense.

You need a boolean as the final evaluation of your if (....) statement.
Try something like this if myFunction will continue to return an int
Expand|Select|Wrap|Line Numbers
  1. if (myFunction(3,5) != 0)
  2. {
  3.  // Then do this
  4. }
Otherwise, you can change myFunction to return a bool depending on the conditions.
Oct 19 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

22
27897
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 = (( curRow == System.DBNull.Value ) ? SqlInt32.Null : (int)curRow ); The errors are (in turn):
2
6152
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 interface method types and the actual implementation types, so I removed all methods from both the interface and the implementation (pArrayStack ) but still get the error. Here is the code with just one simple method still declared and implemented.
2
6737
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 = "Username already exists!" Else lblStatus.Text = "Success!"
6
30716
by: eric.goforth | last post by:
What's wrong with this? if (bool)(UserIDs == ThisUserId) { return true; }
6
16999
by: Doug | last post by:
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
2
3661
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 for (int i = 0; i < p.Length; i++) { command.Parameters.Add(p); if (p.Direction = ParameterDirection.InputOutput) <<<==
7
16708
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 returned from the source. My first attempt was as follows; (please ignore that error handling is not present in this example) public T GetValue<T(string objName) { T results;
6
30194
by: John | last post by:
The following code: int test = 1; bool isTrue = (bool)test; results in a compiler error: Cannot convert type 'int' to 'bool' wtf, any ideas on how to work around this?
4
6752
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 == "Tibia") {
0
9568
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9399
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10163
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10007
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8832
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6649
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5276
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.