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 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: - int myFunction(int a, int b) {
-
if (a < b) return -1;
-
if (a == b) return 0;
-
if (a > b) return 1;
-
}
-
-
// ...later in your code...
-
-
if (myFunction(3, 5)) {
-
// ...
-
}
-
-
// OR
-
-
while (myFunction(x, 3)) {
-
// ...
-
}
C and C++ allow this, maybe C# doesn't?
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)
Please post the piece of code where the exceptiong is thrown
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: - int myFunction(int a, int b) {
-
if (a < b) return -1;
-
if (a == b) return 0;
-
if (a > b) return 1;
-
}
-
-
// ...later in your code...
-
-
if (myFunction(3, 5)) {
-
// ...
-
}
-
-
// OR
-
-
while (myFunction(x, 3)) {
-
// ...
-
}
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.
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 - if (myFunction(3,5) != 0)
-
{
-
// Then do this
-
}
Otherwise, you can change myFunction to return a bool depending on the conditions.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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):
|
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.
|
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!"
|
by: eric.goforth |
last post by:
What's wrong with this?
if (bool)(UserIDs == ThisUserId)
{
return true;
}
|
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
| |
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) <<<==
|
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;
|
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?
|
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")
{
|
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...
|
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,...
| |
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...
|
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...
|
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...
|
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();...
|
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...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |