C#, “?” and “:” operators
C#, “?” and “:” operators
I not found any solution for my problem so i ask, how ? and : operators works when i have multiple statemants?
?
:
What i want to do, i have pixel on the middle pixel[pos] and pixels around, its look like:
pixel[pos]
0 0 0
0 x 0
0 0 0
x is a center pixel.
x
Im checking, if i have any white (zero) pixel around it. If is anyone, i marked pixel as two. If not, so pattern looks like:
zero
two
1 1 1
1 x 1
1 1 1
1 is an black pixel, i set it to one.
1
one
Now, the code:
if(pixels[positionOfPixel] == one && x > 0 && x < width
&& y > 0 && y < height)
pixels[positionOfPixel] = pixels[positionOfPixel - 1] == zero ? two :
pixels[positionOfPixel] = pixels[positionOfPixel + 1] == zero ? two :
pixels[positionOfPixel] = pixels[positionOfPixel - offset] == zero ? two :
pixels[positionOfPixel] = pixels[positionOfPixel + offset] == zero ? two :
pixels[positionOfPixel] = pixels[positionOfPixel - offset + 1] == zero ? two :
pixels[positionOfPixel] = pixels[positionOfPixel + offset - 1] == zero ? two :
pixels[positionOfPixel] = pixels[positionOfPixel - offset - 1] == zero ? two :
pixels[positionOfPixel] = pixels[positionOfPixel - offset + 1] == zero ? two : zero;
My question is, why every one pixel is marked as two ? Why it didnt recognize pixel, where every pixel arond is one (like in second pattern)?
one
two
one
Thanks for any advices!
There is only the
?: operator; not two..– user2864740
Aug 26 at 19:05
?:
a?b:c is 1 operator with 3 operands.– Henk Holterman
Aug 26 at 19:05
a?b:c
And the guideline is to use it sparingly and not to nest it. You are nesting it 8 times, with embedded assignments for extra unreadability. This is not a good way to code.
– Henk Holterman
Aug 26 at 19:07
Is ternary the right choice here? You've obfuscated the code from even yourself. I don't think your implementation would suffer much with traditional
if statements– Parrish Husband
Aug 26 at 19:07
if
1 Answer
1
I'm not a C# specialist, but there is common rule how ? : operator can be used.
x = (boolean condition) ? reult_if_true : result_if_false;
E.g.
drink = isThisPersonAGirl ? wine : beer;
If you want to use many conditions with ? : operator, you should do it that way:
x = (boolean condition 1) ? result_if_true : (boolean condition 2) ? result_if_bool_2_is_true : result_if false;
E.g.
drink = isThisPersonAChild ? lemonade : isThisPersonAGitl ? wine : beer
In your code snippet, it's hard to understand what's going on because you use = operator too often. In most languages, you could initalize several variables like this:
a = b = c = 0, so a, b, c will be = 0;
So I think your mistake is using = operator too often so maybe only this condition does matter, while others are simply skipped:
pixels[positionOfPixel - offset + 1] == zero ? two : zero;
Sorry of it doesn't help, since I'm really not a C# coder)
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
what's your really question?
– D-Shih
Aug 26 at 19:03