views:

300

answers:

7

I can't seem to find what I need on google, and bet I'll get quick answer here.

    String str;
    bool b = true;
    b ? str="true" : str="false";

    Console.Out.WriteLine(str);

that ? : syntax looks correct to me. I'm getting compiler error though.

Program.cs(13,28):
error CS1002: ; expected
Program.cs(13,28):
error CS1525: Invalid expression term ':'
Program.cs(13,30):
error CS1002: ; expected

Not sure about the csharp syntax, but that builds in cpp. Please help! thanks!

UPDATE: About 10 of you give the correct answer LOL, so I'll just award to the first person who submitted it.

interesting Syntax, and I think I actually like it better than c++ syntax.

The actual code I was doing this for is:

ftp.ConnectMode = job.FTPUsePassiveMode ? FTPConnectMode.PASV : FTPConnectMode.ACTIVE;
+15  A: 

Your code should read:

str = b ? "true" : "false";

However, this is akin to just calling b.ToString().ToLower(). That said, I suspect your actual use-case is a little more complex than just converting the Boolean value to a string.

Update
As indicated in the comments, the conditional operator returns a value; it is not for control flow like if/else.

Jeff Yates
The relevant point is that the ternary operator (?:) is an expression that returns a value, not a control flow mechanism (like if/else).
Tyler McHenry
@Tyler: Well said.
Josh
+14  A: 
str = b ? "true" : "false";

But you could just do this:

str = b.ToString();

Or even cut out the middleman altogether:

Console.WriteLine(b);

HTH,
Kent

Kent Boogaart
Strictly speaking, you'd get "True" and "False" from ToString() (and thus WriteLine), not "true" and "false", but I'm not sure it would matter to me. You could always do ToString().ToLower().
tvanfosson
+1  A: 
str = (b) ? "true" : "false";
RC
But... Why enclose `b` in parentheses ? I fail to see the ambiguity it resolves.
RaphaelSP
+3  A: 

The ternary operator doesn't allow for statement switching, only value switching. You want to do this:

str= b ? "true" : "false"

(obviously b.ToString()) is a better solution for this particular problem, but I'm assuming this is just an example).

Adam Robinson
expression selection... and str="true" IS an expression. Try a = b? (str="true"): (str="false"); and I think you'll find that it works just fine.
Ben Voigt
And to address the point that this is valid c++ code, that's because you can use statements to get values in c++. So the following would be valid in c++ (although very confusing): `a = b ? c=d : c=f`
Josh
@Ben: Yes, expression selection is a better term, and an assignment is an expression (returning the value placed into the lvalue), but the output of the ternary operator can't be ignored.
Adam Robinson
+2  A: 

What everyone else said, and: http://msdn.microsoft.com/en-us/library/ty67wk28.aspx

Yoopergeek
and http://en.wikipedia.org/wiki/Ternary_operation
jschmier
A: 

The ternary operator can't be the top-level of a statement in C#, because C# requires that top-level expressions have a side-effect.

Ben Voigt
A: 

Just out of curiosity, why not just do this:

bool b = true;
string str = b.ToString();

In .NET, value types automatically convert their value to a string when .ToString() is called...including booleans.

jrista
while that's a good suggestion it doesn't answer the question of why the ternary operator isn't working in this case.
Josh
The point was to eliminate the problem completely. Other answers adequately explained why it wasn't working (because he was using it incorrectly.) I don't like to repeat answers, especially when there are a bunch of the same. I don't think this deserved a down vote, as it was a valid solution to the problem, even if it didn't explicitly answer his question.
jrista