Zigma
Zigma

Reputation: 529

returning Boolean values

I have a piece of code which is to be converted to c#.

bool checkvalue()
{
unsigned char ucvalue;
Method(&ucvalue);
return ucvalue? false:true;
}

the Method() has the definition :

This function returns the current position . 0 = OFF 1 = ON

So I didn't get what return ucvalue? false:true; means.

Thanks.

Upvotes: 0

Views: 139

Answers (3)

user2672165
user2672165

Reputation: 3049

It is equivalent to:

return ucvalue==0;

which I find the most attractive form. I would perhaps change Method() so that it return the value instead of taking an argument. That will make the code simpler:

return Method()==0;

Upvotes: 2

athabaska
athabaska

Reputation: 455

I'd presume it means "if ucvalue is null, return false, else return true"

Upvotes: 1

bash.d
bash.d

Reputation: 13217

This the ternary-operator, you can "translate" this expression almost 1:1 to C#. It is the same like

if(ucvalue)
   return false;
return true;

From MSDN for C#:

The conditional operator (?:) returns one of two values depending on the value of a Boolean expression. Following is the syntax for the conditional operator.

condition ? first_expression : second_expression;

From here for C++:

You can exchange simple if-else code for a single operator – the conditional operator. The conditional operator is the only C++ ternary operator (working on three values). Other operators you have seen are called binary operators (working on two values).

Upvotes: 3

Related Questions