7

I've probably been working too hard, but can someone explain to me the following, taken from the Immediate window?

(int)DateTime.Now.Date.DayOfWeek = 4

and

(int)DayOfWeek.Sunday = 0

and

(int)DateTime.Now.Date.DayOfWeek - (int)DayOfWeek.Sunday = 4

but
(int)DayOfWeek.Sunday - (int)DateTime.Now.Date.DayOfWeek = Could not evaluate expression`

Thanks for reading.

EDIT:

Its the Immediate window that's giving me this weird result, not regular code.

Screenshot: http://ploader.net/files/0c2556df475b3075634d7fd2b0575794.PNG

EDIT2:

The community seem to think its a bug in VS2010. I wonder if @EricLippert or @JonSkeet could spare a minute to confirm this or, if its not, offer an explanation about this behaviour?

immutabl
  • 6,857
  • 13
  • 45
  • 76
  • This works fine for me. Can you post all of your code? Perhaps there is something else going on here? – Jay Jul 14 '11 at 16:16
  • Its not code, just the output of ad hoc expressions in the Immediate window. – immutabl Jul 14 '11 at 16:18
  • 1
    @Jay My guess is that he is talking about the immediate window when debugging - I've tried it and it doesn't work, but I have no idea why. – Justin Jul 14 '11 at 16:19
  • Oh i see now. The immidate window wouldn't know what to do if you just paste that expression into it. If you'd like it to print the Debugger.Print(); or set a var x = ; but just saying x-y in that window doesn't do anything. So the exception is thrown – Jay Jul 14 '11 at 16:22
  • hmm. But if the operands are reversed it works fine doesn't it? I am baffled. Where's Jon Skeet when you need him ...? – immutabl Jul 14 '11 at 16:23
  • 1
    Oh your right, I missed that. Seems very odd. – Jay Jul 14 '11 at 16:28
  • Are you really asking the reason 0 - 4 can not be expressed as a valid DateTime operation? Try taking the absolute value of that expression. I understand this only a problem within the Immediate windows ( I sadly cannot access your image ) honestly it makes total sense to me. You cannot express a day as a negative number. – Security Hound Jul 14 '11 at 16:31
  • 1
    @Raymond He is casting to an int to be expressed as an int...all other variations work. – Aaron McIver Jul 14 '11 at 16:33
  • @Raymond - as @AaronMcIver points out, its nothing to do with Datetime - I'm casting enums and subtracting one from the other. - try it yourself during a debug session. Its only a specific order of the operands (zero first) in the calculation that doesn't work. – immutabl Jul 14 '11 at 22:37

2 Answers2

5

It looks specific to the constant 0 and a non-literal value. The following works just fine:

int zero = 0;
zero - (int)DateTime.Now.Date.DayOfWeek
-4

While the following fails:

int four = 4;
0 - four
Could not evaluate expression

Update: I couldn't find a similar bug report, so I created one: https://connect.microsoft.com/VisualStudio/feedback/details/679501/integer-literal-0-integer-variable-could-not-evaluate-expression-immediate-window

Update #2: Microsoft is able to reproduce the issue and has resolved it as "Won't Fix", meaning there's hope for the next version of Visual Studio, but not for VS2010.

Joel Rondeau
  • 7,486
  • 2
  • 42
  • 54
  • I'd agree with that. Surely its a bug? Do I get some sort of prize for bringing this to the attention of the eggheads on the Visual Studio project team? ;-) – immutabl Jul 14 '11 at 22:37
  • 1
    Probably not, but if Eric Lippert reads this, you might get it fixed in the next release. – Joel Rondeau Jul 15 '11 at 00:09
3

I have no idea, it looks like a bug to me.

// This doesn't work
0 - (int)DateTime.Now.Date.DayOfWeek

// But loads of similar variations do:
1 - (int)DateTime.Now.Date.DayOfWeek
-1 - (int)DateTime.Now.Date.DayOfWeek
a - (int)DateTime.Now.Date.DayOfWeek
0 - (int)DayOfWeek.Thursday

In any case everything behaves as expected in the compiled code.

Justin
  • 84,773
  • 49
  • 224
  • 367