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?
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?
This code is equivalent to:
if(layout == ProductLayout.Grid4Columns)
repeater.StartIndex = startIndex - 1;
else
repeater.StartIndex = 0;
Hope that helps
EDIT:
The conditional operator ?: is the only ternary operator in C# (I believe). The first operand is a boolean value, if this is true, the value of the whole expression is the second term, otherwise, the value of the entire expression is the third term.
value = (condition) ? (valueIfConditionIsTrue) : (valueIfConditionIsFalse)
The ? is a conditional operator that states if condition then x else y on a single line
int x = ( condition ) ? 0 : 1;
Would result in x = 0 if condition == true or x = 1 if condition == false
It is called ternary operator. More information at the following link.
http://msdn.microsoft.com/en-us/library/ty67wk28%28VS.80%29.aspx
Just break it into pieces:
repeater.StartIndex = layout == ProductLayout.Grid4Columns ? startIndex - 1 : 0;
repeater.StartIndex = (layout == ProductLayout.Grid4Columns ? startIndex - 1 : 0);
bool criteria = (layout == ProductLayout.Grid4Columns);
repeater.StartIndex = criteria ? startIndex - 1 : 0;
In other words, if layout is equal to Grid4Columns, it will set start index to "startIndex - 1", otherwise, it will set StartIndex to 0.
Equivalent to
if(layout == ProductLayout.Grid4Columns)
{
repeater.StartIndex = startIndex - 1;
}
else
{
repeater.StartIndex = 0;
}
Unless I'm mistaken and this does something else in C# than in PHP, that's a ternary expression, which written in verbose form would be:
if ( layout == ProductLayout.Grid4Columns ) { repeater.StartIndex = startIndex - 1; } else { repeater.StartIndex = 0; }
It is not chained just simple use of ?: Much more readable than
if(layout == ProductLayout.Grid4Columns)
{
repeater.StartIndex = startIndex - 1 ;
}
else
{
repeater.StartIndex = 0;
}
I don't like it... I feel it's fairly ambiguous about ordering. It's not clear what was intended, and if that matches how it's actually executed.
Is it
repeater.StartIndex = (layout == ProductLayout.Grid4Columns) ? startIndex - 1 : 0;
or
repeater.StartIndex = layout == (ProductLayout.Grid4Columns ? startIndex - 1 : 0);
Things could get screwy if operator precedence is not what you expect, and you're unlucky enough that the types are compatible.
Add the appropriate bracketing, though, and I have no problem with it.
Although the syntax seems hackish at first glance, after you get used to it it becomes a great shortcut for simple things like null substitution.
public myNormalizedString = someString ? null : "";
When I write a statement like this, I add parentheses to make the intent of the statement clearer:
repeater.StartIndex =
(layout == ProductLayout.Grid4Columns ? startIndex - 1 : 0);
repeater.StartIndex = layout == ProductLayout.Grid4Columns ? startIndex - 1 : 0;
layer == [expression] => [expression]
What this means that the above statement can be treated like a black back reducing to the following expression:
repeater.StartIndex = (some logic)?0:n ,where n is some value greater than zero. The purpose of n begin a relative position in the grid.
Note, layout holds a reference to a instance of ProductLayout.member. All the code means is that in the special case (i.e. when setting the repeater's startIndex when the layout type is Grid4Columns) then the start index is not 0.