Hi,
Is there substitution for booleans in C#? If bool 1 false then return bool 2 and the opposite? And what to do if there is more then two boolean values - Coalesce, NullIf...?
Thank you very much.
Art.
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Sam HobbsPosted Nov 24, 2010, 6:10 PM
but as best as I understand it, you can use:
However I think the following will produce the same results:
ArthurPosted Nov 24, 2010, 3:01 PM
Art.
B ListPosted Nov 24, 2010, 2:41 PM
IF address1 is not null THEN
result := address1;
ELSIF address2 is not null THEN
result := address2;
ELSIF address3 is not null THEN
result := address3;
ELSE
result := null;
END IF;
could translate to the following in C# (or C++) (I'm going to assume the addresses are of string type and you really mean blank when you indicate "null")
string address1 = "";
string address2 = "";
string address3 = "";
string result;
result = (address1.Length > 0) ? address1 : (address2.Length > 0) ? address2 : (address3.Length > 0) ? address3 : "";
The conditional operator (?) is often used as a shortcut to the if..else statement.
Ex.
if (A == 1) test = "A is 1"
else test = "A is not 1";
is the same as
test = (A == 1) ? "A is 1" : "A is not 1";
ArthurPosted Nov 24, 2010, 1:42 PM
Any more examples would be appreciated.
I'm looking for smth that would work lk this:
if bool1 false then return bool2... else return bool1.
I was hoping there is some kind of single line, no coding required function/method/statement in C# to do this. I can write if but trying to find out if there is an easier way to manipulate with values with minimal coding. I can only give some SQL example:
SELECT coalesce( address1, address2, address3 ) address FROM suppliers;
This single line built-in SQL function is similar to what's below. With minimal coding as above one can do this:
IF address1 is not null THEN
Thank you very much.
Art
B ListPosted Nov 24, 2010, 12:44 PM
int s = (w == null) ? 0 : Convert.ToInt32(w);
If not, then maybe give a brief overview of what you want to use the booleans for.
ArthurPosted Nov 24, 2010, 10:55 AM
But is there a single line function/statement in C# that can do the same and be used with boolean types/expressions? I'm looking for smth similar to built-in SQL functions: NVL(string, replace_string) and/or Coalesce(expr1, expr2...... expr n)? Do I have to actually write my own functions for this? And if I have to write my own then how to program a single function/method that would work with numbers, strings and boolean types please? If number then return a number, string-string, bool- return t or f...
If there is a built-in functionality in C# that can do the same please refer me to this and/or give some exact syntax examples please.
Thank you very much.
Art.
Jiteendra SampathiraoPosted Nov 24, 2010, 6:38 AM
function asdasdad()
{
int s=someoperation;
return s;
}
if (s==0)
{
//do something
}
if(s==1)
{
//do something
}
is(s==2)
{
//do something
}
You can assign it to strings also.
If this post helped you, then tick the "Do you like this Answer" checkbox.