Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert "0" and "1" to false and true

I have a method which is connecting to a database via Odbc. The stored procedure which I'm calling has a return value which from the database side is a 'Char'. Right now I'm grabbing that return value as a string and using it in a simple if statement. I really don't like the idea of comparing a string like this when only two values can come back from the database, 0 and 1.

OdbcCommand fetchCommand = new OdbcCommand(storedProc, conn);  fetchCommand.CommandType = CommandType.StoredProcedure; fetchCommand.Parameters.AddWithValue("@column ", myCustomParameter); fetchCommand.Parameters.Add("@myReturnValue", OdbcType.Char, 1)             .Direction = ParameterDirection.Output; fetchCommand.ExecuteNonQuery();  string returnValue = fetchCommand.Parameters["@myReturnValue"].Value.ToString(); if (returnValue == "1") {     return true; }  

What would be the proper way to handle this situation. I've tried 'Convert.ToBoolean()' which seemed like the obvious answer but I ran into the 'String was not recognized as a valid Boolean. ' exception being thrown. Am I missing something here, or is there another way to make '1' and '0' act like true and false?

Thanks!

like image 225
Chris Avatar asked Apr 15 '10 18:04

Chris


People also ask

How do you convert False to zero?

int() turns the boolean into 1 or 0 . Note that any value not equal to 'true' will result in 0 being returned.

Why is 1 true and 0 False Python?

The Python Boolean type is one of Python's built-in data types. It's used to represent the truth value of an expression. For example, the expression 1 <= 2 is True , while the expression 0 == 1 is False . Understanding how Python Boolean values behave is important to programming well in Python.

Is 0 or 1 true or False in C?

C does not have boolean data types, and normally uses integers for boolean testing. Zero is used to represent false, and One is used to represent true. For interpretation, Zero is interpreted as false and anything non-zero is interpreted as true.


1 Answers

How about:

return (returnValue == "1"); 

or as suggested below:

return (returnValue != "0"); 

The correct one will depend on what you are looking for as a success result.

like image 153
kemiller2002 Avatar answered Oct 13 '22 19:10

kemiller2002