conditional-operator

Why does the Perl conditional operator not do what I expect?

This snippet of Perl code in my program is giving the wrong result. $condition ? $a = 2 : $a = 3 ; print $a; No matter what the value of $condition is, the output is always 3, how come? Edit: I wonder if Perl is alone in this regard. Does your favorite language suffer from this bug/feature ? ...

Conditional operator assignment with Nullable<value> types?

EmployeeNumber = string.IsNullOrEmpty(employeeNumberTextBox.Text) ? null : Convert.ToInt32(employeeNumberTextBox.Text), I often find myself wanting to do things like this (EmployeeNumber is a nullable as it's a property on a LINQ-to-SQL dbml object where the column allows NULL values). Unfortunately, the compiler fe...

In C# why can't a conditional operator implicitly cast to a nullable type

I am curious as to why an implicit cast fails in... int? someValue = SomeCondition ? ResultOfSomeCalc() : null; and why I have to perform an explicit cast instead int? someValue = SomeCondition ? ResultofSomeCalc() : (int?)null; It seems to me that the compiler has all the information it need to make an implicit casting decision, n...

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...

Nullable type issue with ?: Conditional Operator

Could someone explain why this works in C#.NET 2.0: Nullable<DateTime> foo; if (true) foo = null; else foo = new DateTime(0); ...but this doesn't: Nullable<DateTime> foo; foo = true ? null : new DateTime(0); The latter form gives me an compile error "Type of conditional expression cannot be determined because there is no ...

How do I use the conditional operator?

I've always wondered how to write the "A ? B : C" syntax in a C++ compatible language. I think it works something like: (Pseudo code) If A > B C = A Else C = B Will any veteran C++ programmer please help me out? ...

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...

Coalesce operator and Conditional operator in VB.NET

Hi guys, Can we use Coalesce operator(??) and conditional ternary operator(:) in VB.NET as in C#? ...

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 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...

Conditional Operators in Javascript

Is it ok to use conditional operators like a statement like so? (x == y) ? alert("yo!") : alert("meh!"); Or is it more correct to use it to assign a value like so? z = (x == y) ? "yo!" : "meh!"; If it's not incorrect to use it like a statement, then is it possible to add more than one line of code for execution like so? Is it more ...

Java short style if evaluation

I can't find the relevant portion of the spec to answer this. In a conditional operator statement in Java, are both the true and false arguments evaluated? So could the following throw a NullPointerException Integer test = null; test != null ? test.intValue() : 0; ...

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

Can someone please explain what the "?" and ":" operators are in PHP? e.g.: (($request_type == 'SSL') ? HTTPS_SERVER : HTTP_SERVER) ...

Conditional operator differences between C and C++

I read somewhere that the ?: operator in C is slightly different in C++, that there's some source code that works differently in both languages. Unfortunately, I can't find the text anywhere. Does anyone know what this difference is? ...

Conditional operator with only true statement

I want to set a variable to a value, but only if a condition is true. Instead of doing the following: if($myarray["foo"]==$bar){ $variablename=$myarray["foo"]; } This can end up being quite long if the variable names are long, or perhaps it involves arrays, when it's quite simple what I want to do set a value if a condition ...

Visually Optimizing JavaScript Conditional Operators that are stringed together

In JavaScript let's say we have the following code var test = 'd'; if (test != 'a' && test != 'b' && test != 'c') alert('were good to go'); This if seems rather lengthy to me. I would love to write something like if (test != ('a' && 'b' && 'c') alert('were good to go'); Sadly this doesn't work. What is a more elegant way to w...

C# conditional AND (&&) OR (||) precedence

We get into unnecessary coding arguments at my work all-the-time. Today I asked if conditional AND (&&) or OR (||) had higher precedence. One of my coworkers insisted that they had the same precedence, I had doubts, so I looked it up. According to MSDN AND (&&) has higher precedence than OR (||). But, can you prove it to a skeptical co...

PHP Conditional Operator and Self Assignment

Is this sort of thing considered OK in PHP? $foo = $_GET['foo']; $foo = empty($foo) || !custom_is_valid($foo) ? 'default' : $foo; Are there cleaner alternatives to this? I'm basically trying to avoid extra table look-ups. ...

why can't conditional operator be used as a statement

Why can't the conditional operator be used as a statement? I would like to do something like: boolean isXyz = ...; ... isXyz ? doXyz() : doAbc(); where doXyz and doAbc are return void. Note that this is not the same as other operators, for example doXyz() + doAbc() intrinsically needs that doXyz and doAbc return a number-like someth...