ternary-operator

C# ?: Conditional Operator

I have this extract of C# 2.0 source code object valueFromDatabase; decimal result; valueFromDatabase = DBNull.Value; result = (decimal)(valueFromDatabase != DBNull.Value ? valueFromDatabase : 0); result = (valueFromDatabase != DBNull.Value ? (decimal)valueFromDatabase : (decimal)0); The first result evaluation throws an invalid cast...

Simple PHP isset test

This below does not seem to work how I would expect it, event though $_GET['friendid'] = 55 it is returning NULL <?PHP $_GET['friendid'] = 55; $friendid = (!isset($_GET['friendid'])) ? $_GET['friendid'] : 'empty'; echo $friendid; exit; ?> ...

PHP syntax question: What does the question mark and colon mean?

Possible Duplicate: quick php syntax question return $add_review ? FALSE : $arg; What do question mark and colon mean? Thanks ...

Can you explain what this code fragment is doing?

repeater.StartIndex = layout == ProductLayout.Grid4Columns ? startIndex - 1 : 0; I don't get it. What is layout == [expression] doing here in this case as it sets StartIndex Can somebody help explain what this code is doing? ...

Reference initialization in C++

Greetings, everyone! Examining my own code, I came up to this interesting line: const CString &refStr = ( CheckCondition() ) ? _T("foo") : _T("bar"); Now I am completely at a loss, and cannot understand why it is legal. As far as I understand, const reference must be initialized, either with r-value or l-value. Uninitialized referenc...

Ternary operator and string concatenation quirk?

Hi I just want to know why this code yields (at least for me) an incorrect result. Well, probably i'm in fault here $description = 'Paper: ' . ($paperType == 'bond') ? 'Bond' : 'Other'; I was guessing that if paperType equals 'Bond' then description is 'Paper: Bond' and if paperType is not equals to 'Bond' then description is 'Paper...

Please explain C# syntax to a vb-er

Hello, I have the following code snippet: // Notify the source (the other control). if (operation != DropOperation.Reorder) { e = new DroppedEventArgs() { Operation = operation == DropOperation.MoveToHere ? DropOperation.MoveFromHere : DropOperation.CopyFromHere, Source = src, Target = this, DroppedItems = src...

Remembering the Ternary Operator Syntax

Anyone have a good trick to remember the standard ternary syntax? Specifically whether the '?' or ':' comes first. I have consistently gotten this backwards over the years. ...

Ternary operator evaluation order

class Foo { public: explicit Foo(double item) : x(item) {} operator double() {return x*2.0;} private: double x; } double TernaryTest(Foo& item) { return some_condition ? item : 0; } Foo abc(3.05); double test = TernaryTest(abc); In the above example, why is test equal to 6 (instead of 6.1) if some_condition is true? Ch...

PHP ternary operator (short if statement) help

Hay All, I've converting a lot of my code to the short hand IF statement to cut down loading time/code. Most of these have been IF statements with ELSE's. However i can see to get it to work on just plain IF statements ($this->left_eye_sph >= 0) ? $this->transpose_left_eye() Any suggestions? ...

Learning by example - terminology (?, :, etc)

When you were a kid, did you ever ask your parents how to spell something and they told you to go look it up? My first impression was always, "well if could look it up I wouldnt need help spelling it". (yeah yeah I know phonetics) ...anyway, I was just looking at some code and I found an example like: txtbx.CharacterCasing = (checkb...

What does question mark in PHP represent other than Ternary Operator?

Is there any other use of the question mark ? in PHP other than being part as the Ternary Operator. Just to take note: I know about how it works in regex and all that. I am talking about the PHP language itself, not regex or what. I know how it works in opening and closing tags. I am more concerned about tokenizing a PHP script. Is the...

Do ternary operators tend to bug / defect injection?

A discussion has come up in my office about the use of ternary operators. There are two sides to this discussion. Side 1) That ternary operators are easy to write and read, therefore convenience is a net cost-savings. Side 2) That ternary operators are difficult to maintain because they require excess code-churn should they ever need t...

PHP syntax question

I found this line of code and I'm trying to comprehend what it's doing. The part I'm not familiar with is the question mark and the colon. What are these characters used for? $string = $array[1] . ($array[0] === 47 ? '' : ' word'); ...

Comparison of conditional statements.

Are following ways are same.(Considering the evaluation time) if(Condition1) { //code1 } else { //code2 } condition1 ? code1 : code2 Are they just syntactically different? It may seem a stupid question.But I want to clear my doubt. ...

Are there any good reasons why ternaries in C# are limited?

Fails: object o = ((1==2) ? 1 : "test"); Succeeds: object o; if (1 == 2) { o = 1; } else { o = "test"; } The error in the first statement is: Type of conditional expression cannot be determined because there is no implicit conversion between 'int' and 'string'. Why does there need to be though, I'm assigning those val...

No implicit int -> short conversion in ternary statement

short s; s = (EitherTrueOrFalse()) ? 0 : 1; This fails with: error CS0266: Cannot implicitly convert type 'int' to 'short'. An explicit conversion exists (are you missing a cast?) Can anyone explain why this is so? The only thing I can think of is that the compiler doesn't look at the second value and doesn't know the range...

Which ternary operator in C# is most popular and mostly used?

Which ternary operator in C# is most popular and mostly used? ...

How to use ternary operator for this statement in c#

int five = 5; when the variable five is equal to 5, write true when the variable five is not equal to 5, write false How do I write a statement for this in ASP.NET using C#? ...

javascript if alternative

what does this bit of code represent? I know its some kind of if alternative syntax .. pattern.Gotoccurance.score != null ? pattern.Gotoccurance.score : '0' tnx QUESTION UPDATE : why need for this sort of coding, is this more efficient or just shortened version with same efficiency? ...