Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 229475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:46:08+00:00 2026-05-11T19:46:08+00:00

Why is it that the following code won’t work: endDate.AddDays(7-endDate.DayOfWeek); While this will: endDate.AddDays(0-endDate.DayOfWeek

  • 0

Why is it that the following code won’t work:

endDate.AddDays(7-endDate.DayOfWeek);

While this will:

endDate.AddDays(0-endDate.DayOfWeek + 7);

?

(By “won’t work” I mean results in the following compilation error: “cannot convert from ‘System.DayOfWeek’ to ‘double'”)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T19:46:08+00:00Added an answer on May 11, 2026 at 7:46 pm

    To expand upon what Lasse said (or rather, make it a little more explicit).

    Because 0 is convertable to an Enum type,

    0 - endDate.DayOfWeek becomes 
    (DayOfWeek)0 - endDate.DayOfWeek
    

    And since you can subtract one enum from another and get an integer difference:

    (DayOfWeek)0 - endDate.DayOfWeek == (int)endDate.DayOfWeek
    

    Thus, since the result of the subtraction is an int, you can then add 7 to it.

    endDate.AddDays(0-endDate.DayOfWeek + 7);
    

    So, if Monday’s Enum value is 1

    0 - endDate.DayOfWeek == -1 + 7 == 6
    

    However, you can’t do the reverse.

    endDate.DayOfWeek - 0 + 7, 
    

    because the result type of the calculation is dependant upon the leftmost side. Thus, while 0 – endDate.DayOfWeek results in an integer, endDate.DayOfWeek – 0 results in an enum DayOfWeek.

    Most interestingly, you could use this side-effect to get the value of an enum without casting, though I would consider this hackish and confusing… thus to be avoided.

    int enumValue = -(0 - endDate.DayOfWeek);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following code (that doesn't work): while True: # Snip: print out current
I understand that the following code won't work, as i is a runtime parameter
Any reason why the following code won't work? $dbconnection = db::getInstance(); //this is a
I understand that the following code won't work Float a=3 because its translated as
I have the following code that won't compile and although there is a way
In this thread some one commented that the following code should only be used
I have following code that does not work due to a being a value
i have the following code that won't compile because of generic problems. It comes
I found that some code won't work unless declared on the header file itself.
This site claims that set_union is equivalent to the following code: template <class InputIterator1,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.