Question

below is simple ternary operator.

 hc.colspan= ( hc.colspan == 1 && hierarchycount == 0) ? hc.colspan : hc.colspan +1 

I need to make this more simple instead of assigning the same value to hc.colspan when the condition is true.

How can I do this without assigning same value to hc.colspan when the condition is true.

like consider another example

a = a!=0 ? a : b

Above we are assigning same a value to "a" again when the condition comes to true. But I don't want to assign it again. How can I do this in a different manner?

Was it helpful?

Solution

You'll have to remove the ternary operator and go with a normal if statement:

if(!(hc.colspan == 1 && hierarchycount == 0)){
    hc.colspan += 1;
}

or, inversed per MarkO's suggestion:

if(hc.colspan != 1 || hierarchycount != 0){
    hc.colspan += 1;
}

OTHER TIPS

Why not simply

if(!(hc.colspan == 1 && hierarchycount == 0))
     hc.colspan++;

For your first example you could do this:

hc.colspan += (hc.colspan == 1 && hierarchycount == 0) ? 0 : 1;

But generally, if you only want to change the value in one case, just use an if clause.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top