Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've googled around and have not found documentation of this particular syntax.

Consider the following:

If m_iPage is indeed 7 then StartLight gets set to 1 and EndLight gets set to 4 as desired.
However if m_iPage is say, 0, the StartLight gets set to 0 but EndLight is set to 4, where I
really needed EndLight to be set to 0 also.

(m_iPage != 7) ? StartLight = m_iPage, EndLight = m_iPage : StartLight = 1, EndLight = 4; 


This is not correct?

Thanks for the help in advance
Posted
Updated 2-Mar-13 6:19am
v3
Comments
Philippe Mori 5-Mar-13 23:21pm    
To understand why it does not works, you have to search information on comma operator and operator precedence instead of ternary operator. Since comma operator has less priority, last part of your expression is always executed as it is not part of the ternary expression.

While ProgramFOX's answer is correct, it is precisely this sort of problem where it would be recommended to stick with an if/else statement. It will make the code easier to read and understand what you are doing, both for yourself when you come back to it later, as well as another developer if they have to take the work on.

Furthermore, whether you take that advice or not, you really would be better off having your condition in the positive form (since you are doing something in both cases) because "if this, do x, otherwise do y" is always easier logic to follow than "if not this, do x, otherwise do y".

I.e.
C++
(m_iPage == 7) ? (StartLight = 1, EndLight = 4) : (StartLight = m_iPage, EndLight = m_iPage);



But I repeat, you really should look to change this, if possible, to:
C++
if (m_iPage == 7)
{
   StartLight = 1;
   EndLight = 4;
}
else
{
   StartLight = m_iPage;
   EndLight = m_iPage;
}


Regards,
Ian.
 
Share this answer
 
Comments
Thomas Daniels 2-Mar-13 12:40pm    
I agree, +5!
Ron Anders 2-Mar-13 12:45pm    
But if then else is so much less sexy :-)
Philippe Mori 5-Mar-13 22:45pm    
You should never use ternary operator for multiple statements but only for simple expressions and generally, you don't want to do any assignment on the right side of the ?.
Ian A Davidson 2-Mar-13 12:58pm    
When you have been a software engineer as long as I have, you know the purpose isn't to write "sexy" code.
The best code is code that is maintainable.
There are places where the ternary operator is useful, but it would normally be for the simplest of cases. (One example, off the top of my head, may be to decide what value to pass to a base class in the constructor of an object). In your case, go with if/else. You would have saved yourself a lot of time already, and you'll save yourself a lot of pain later. (E.g. what if later you find you need to do something else when m_iPage is/is not 7 - you'd probably have to change to if/else anyway. Or what if you decide you also need to do something when m_iPage is 6, etc).
Regards,
Ian.
JayantaChatterjee 2-Mar-13 13:23pm    
My 5!
Hi,

You have to write parentheses:
C++
(m_iPage != 7) ? (StartLight = m_iPage, EndLight = m_iPage) : (StartLight = 1, EndLight = 4);

Hope this helps.
 
Share this answer
 
Comments
Ron Anders 2-Mar-13 12:20pm    
Yep, that did it. Thanks ProgramFOX!
Thomas Daniels 2-Mar-13 12:21pm    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900