ternary-operator

Why is my ternary expression not working?

I am trying to set a flag to show or hide a page element, but it always displays even when the expression is false. $canMerge = ($condition1 && $condition2) ? 'true' : 'false';...<?php if ($canMerge) { ?>Stuff<?php } ?> What's up?...

How can I closely achieve ?: from C++/C# in Python?

In C# I could easily write the following: string stringValue = string.IsNullOrEmpty( otherString ) ? defaultString : otherString; Is there a quick way of doing the same thing in Python or am I stuck with an 'if' statement? ...

To ternary or not to ternary?

I'm personally an advocate of the ternary operator: () ? : ; I do realize that it has its place, but I have come across many programmers that are completely against ever using it, and some that use it too often. What are your feelings on it? What interesting code have you seen using it? ...

Why is this code invalid in C#?

The following code will not compile: string foo = "bar"; Object o = foo == null ? DBNull.Value : foo; I get: Error 1 Type of conditional expression cannot be determined because there is no implicit conversion between 'System.DBNull' and 'string' To fix this, I must do something like this: string foo = "bar"; Object o = foo == null ?...

FizzBuzz using Ternary Operator

I've been reading up on conditional style expressions in ruby, however I came across one I couldn't quite understand to define the classic FizzBuzz problem. I understand the FizzBuzz problem and even wrote my own before finding the following quick solution utilising the ternary operator. If someone can explain to me how this chain works ...

Which coding style you use for ternary operator?

I keep it in single line, if it's short. Lately I've been using this style for longer or nested ternary operator expressions. A contrived example: $value = ( $a == $b ) ? 'true value # 1' : ( $a == $c ) ? 'true value # 2' : 'false value'; Personally which style you use, or find ...

Lambdas and the ternary operator, weird issue.

Ok, here's the deal. Generally, when using the ternary, here's the syntax: int x = 6; int y = x == 6 ? 5 : 9; Nothing fancy, pretty straight forward right? Now, let's try to use this when assigning a Lambda to a Func type. Let me explain: Func<Order, bool> predicate = id == null ? p => p.EmployeeID == null :p => p.EmployeeID == id; ...

Unique ways to use the Null Coalescing operator

I know the standard way of using the Null coalescing operator in C# is to set default values. string nobody = null; string somebody = "Bob Saget"; string anybody = ""; anybody = nobody ?? "Mr. T"; // returns Mr. T anybody = somebody ?? "Mr. T"; // returns "Bob Saget" But what else can ?? be used for? It doesn't seem as useful as t...

Python Ternary Operator

I was under the impression that Python had a ternary operator... But then I did some research, Not enough to find out for sure though Thought I'd ask the professionals ;) ...

How does the ternary operator work?

Please demonstrate how the ternary operator works with a regular if/else block. Example: Boolean isValueBig = value > 100 ? true : false; Exact Duplicate: How do I use the ternary operator? ...

C# ?? combined with ?: question

I'm building an XML Deserializer for a project and I run across this type of code situation fairly often: var myVariable = ParseNDecimal(xml.Element("myElement")) == null ? 0 : ParseNDecimal(xml.Element("myElement")).Value; Is there a better way to write this statement? EDIT : Perhaps I should have clarified my exam...

Is there a conditional ternary operator in VB.NET?

In Perl (and other languages) a conditional ternary operator can be expressed like this: my $foo = $bar = $buz ? $cat : $dog; Is there a similar operator in VB.NET? Thanks to Lucky and Kris, I now know that in VB.NET this operation is expressed like this: Dim foo as String = IF(bar = buz, cat, dog) ...

Resharper Trap "Convert to 'return' statement"

Given the following routine: private static object ParseNumber(string token, FieldDefinition def) { if (def.Fraction > 0) return Double.Parse(token); else return Int64.Parse(token); } Resharper offers me the option to refactor it into a statement with the ternary operator: private static object ParseNumber(string token, F...

Ternary operator: bad or best practice?

I'm looking for reasons to use/not to use it and for original ideas (in their use and to replace them). Duplicate: To Ternary Or Not To Ternary Related (but does not address the question being asked): Which coding style you use for ternary operator? ...

What do fellow .NET developers think about the conditional operator?

I really really like the conditional operator in C#. It makes my life very much easier in writing logic such as this: public string FormattedFileName { get { return string.Format("{0}_{1}_{2}_{3}.xls", DateTime.Now.Month.ToString().Length == 1 ? "0" + DateTime.Now.Month.ToStrin...

The ternary (conditional) operator in C

What is the need for the conditional operator? Functionally it is redundant, since it implements an if-else construct. If the conditional operator is more efficient than the equivalent if-else assignment, why can't if-else be interpreted more efficiently by the compiler? ...

What does '?' do in C++?

int qempty(){ return(f==r)?1:0;} In the above snippet, what does "?" mean? What can we replace it with? ...

What is the Java ?: operator called and what does it do?

I have been working with Java a couple of years, but up until recently I haven't run across this construct: int count = isHere ? getHereCount(index) : getAwayCount(index); This is probably a very simple question, but can someone explain it? How do I read it? I am pretty sure I know how it works. if isHere is true, getHereCount() i...

iif equivalent in c#

What's the iif equivalent in c#? or similar short circuit? ...

Is it bad to coalesce the evaluator in a ternary expression? (C#)

I've looked around a little and haven't found an equivalent question. Is this bad coding practice? I can read it easily, but is it too cryptic for someone reading the code? bool? testBool = null; string result; result = (testBool ?? false ? "Yes" : "No"); Edit: My apologies to everyone for some horrible code! Here is a working e...