views:

125

answers:

5

This has me pretty stumped. Maybe I'm too tired right now.

    Rectangle rectangle = new Rectangle(0, 0, image.Width, image.Height);
    Rectangle cropArea = inputArea == null ? rectangle : inputArea.Value;

    if (inputArea == null)
        cropArea = rectangle;

inputArea is a nullable Rectangle, which in my particular case is null.

The first two statements yields a cropArea initialized to 0. The second, however, yields the correct cropArea based on the image width and height. Have I misunderstood anything with the conditional operator? It seems it does not return rectangle when inputArea = null? Is there any quirks when working with value types?

EDIT: Alright, I should have tried this first: restarted VS. It seems the debugger lied to me, or something. Anyway, works now. Thanks.

A: 
Rectangle cropArea = (!inputArea.HasValue) ? rectangle : inputArea.Value;
nonnb
That’s equivalent.
Timwi
Now why the spam on *my* thread fellas :)@Hitech Rectangle? inputArea = null; Nullable<Rectangle> inputArea2 = null;@TimWi - now why the downvote .. was just trying to help the OP out?
nonnb
@nonnb: +1 because I made a mistake on your post and started a small flame war. Turns out it is only the Silverlight Rectange class that is not nullable.
Enough already
A: 

Your code appears correct. The conditional expression (or conditional operator, or originally called the ternary operator... everyone happy now? :)) should be interchangeable with if/else statements.

Rectangle cropArea = inputArea == null ? rectangle : inputArea.Value;

should be exactly the same as:

Rectangle cropArea;
if (inputArea == null)
{
    cropArea = rectangle;
}
else
{
    cropArea = inputArea.Value;
}

(in fact they should generate the same IL code).

Trace through with the debugger and see if anything jumps out at you.

Enough already
Please don’t call the conditional operator “the ternary operator”. I’ve edited your answer to correct this, I hope you don’t mind.
Timwi
A: 

So are you saying that when inputArea is null, without the if statement you get a rectangle initialized to something else than the image size? I just tried running that and it works fine. Make sure that image has a size and that inputArea is actually null.

Matti Virkkunen
Yes, all of which had seemed very weird, until I realized visual studio lied to me. See my edit.
Max Malmgren
@Max: Maybe it was a good thing I didn't use VS, then.
Matti Virkkunen
A: 

What the hell?

Rectangle rectangle = ...;
Rectangle cropArea;
if (inputArea == null)
    cropArea = rectangle;
else
    cropArea = inputArea.Value;

if (inputArea == null)
    cropArea = rectangle;

Why have the second if? It's totally and entirely redundant. The scenario in which cropArea might still be null or zero is if inputArea.Value is null/zero, since you didn't check for that (only if inputArea is null).

DeadMG
+1  A: 

That seems like a nasty bug in Visual Studio debug mode which is fooling you:

alt text

Now F10 to step over this line and you get:

alt text

On the console correct values are printed.

WTF.

Darin Dimitrov
Did you try Nullable<Rectangle>
JeremySpouken
@JeremySpouken, for which variable?
Darin Dimitrov
@JeremySpouken: That’s equivalent to `Rectangle?`.
Timwi
Interesting. I typed the same code into VS and I get the correct values (in both DEBUG and RELEASE mode)...
Timwi
@Timwi, what is the exact version number of VS you are using and the OS? For me it's `10.0.30319.1 RTMRel` running on Windows 7 x64.
Darin Dimitrov
@HiTech Magic, do you understand what a nullable type means in .NET and what's the use of `System.Nullable<T>`? From your comments I get the impression that you don't. I would recommend you reading the relevant section from the documentation : http://msdn.microsoft.com/en-us/library/b3h38hb0.aspx
Darin Dimitrov
@Darin: Same here but 32-bit — that may well make the difference. Thanks for digging :)
Timwi
@whomever cares: Quite right. It is System.Windows.Shapes.Rectangle that is not Nullable (i.e. Silverlight). I will try and add lots of pleases etc to ensure everyone does not react so violently to my comments in future :)
Enough already
@HiTech Magic, now that makes things clear :-) Also if you have read the question more carefully you would have noticed that he is using `inputArea.Value` and there's no `Value` property defined in `System.Windows.Shapes.Rectangle` class as well as a constructor taking 4 integer parameters.
Darin Dimitrov