HAdes
HAdes

Reputation: 17033

Why does cast always returns zero only when debugging in VS2010?

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

Upvotes: 5

Views: 150

Answers (3)

James King
James King

Reputation: 6353

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).

Upvotes: 1

Curt Nichols
Curt Nichols

Reputation: 2767

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.)

Upvotes: 0

Denis Palnitsky
Denis Palnitsky

Reputation: 18387

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

Upvotes: 0

Related Questions