views:

104

answers:

3

Only on my machine this happens. Basically if i run the following all is as expected:

        double d = 500.22;
        int i = (int)d;
        Console.WriteLine(i.ToString());

Output is 500.

However if i put a breakpoint on the first line and step through, i always reverts to zero and the output is 0.

I've tested this on other machines and I cannot replicate, I've even reinstalled VS2010 and it still happens. So I'm thinking it must be some sort of environment setting that I have on my system, but I cannot figure out what.

Anyone else had this issue and how do I get rid of it.

Cheers.

EDIT: It appears that the issue is just with the 2nd line. If I put a break point on the 1st line and then just F5 over it, then it's all ok. But if I step into/over the 2nd line, the casting doesn't appear to work and i stays at 0.
I also tried setting i to 1 first and then seeing if the cast works, but it changes i back to 0 and removes my initial value of 1.

Here's a screenshot:

alt text

A: 

Try to run VS with /SafeMode or /ResetSettings command line args

Orsol
thanks for the suggestion, however still the same thing happens.
HAdes
+1  A: 

Do you have all the latest .NET Framework service patches? There are some even for the 4.0 framework, though I haven't heard of anything like this yet.

* Edit *
Regarding my other suggestions, casting to an int truncates, and Convert.ToInt32 rounds, so Convert.ToInt32 is what you generally want anyway (especially if it's working).

James B
I think i'm fully up to date as running the windows update didn't pick anything up.
HAdes
that's a good point, i didn't realise that casting truncates and doesn't round. I always wondered what the diff was. cheers.
HAdes
A: 

Note that in your screen shot you aren't outputting i, you're outputting d--you aren't actually using i. I can't repro the behavior you've described, but I get close if I put a breakpoint on the WriteLine in Release mode: i doesn't even exist in that case. But why should it, you're not using it. (Note I used the code in your screen shot for repro, not the code that actually uses i.)

Curt Nichols
whoops, sorry yeah i made a modification to see what it would do, but still the same issue. Anyway, that is irrelvant as the problem is with the 2nd line not with the console line. I'll try and repo your Release mode issue and see.
HAdes