Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / August 2005

Tip: Looking for answers? Try searching our database.

System.Windows.Form.Day vs System.DayOfWeek

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Sue & Bill - 23 Aug 2005 11:42 GMT
Form's MonthCalendar.FirstDayOfWeek uses System.Windows.Forms.Day.

But CultureInfo's DateTimeFormatInfo.FirstDayOfWeek uses
System.DayOfWeek.

Both are enumerated types listing Friday ... Wednesday (alphabetical
order).

However, when I tried to cast the second to the first to configure a
MonthCalendar control, the result is incorrect (appears offset by one
day).  What is a proper mapping function between the two?

Does .NET Framework 2.0 harmonize the two?

Thanks
Morten Wennevik - 23 Aug 2005 13:02 GMT
Hi Sue and/or Bill,

The Day enumeration and DayOfWeek enumeration is indeed off by 1.  Whereas Day follows the international standard with the first day of the week as Monday (ISO 8601), DayOfWeek does not, and uses Sunday as the first day of the week.

You can easily see the difference by comparing the result of this code using two ListBoxes to output the data:

for(int i = 0; i < 7; i++)
{
    DayOfWeek d1 = (DayOfWeek)i;
    Day d2 = (Day)i;
    listBox1.Items.Add(d1.ToString());
    listBox2.Items.Add(d2.ToString());
}

Intellisense lists members alphabetically regardless of the actual enumeration.
The documentations also lists the items alphabetically.

There is no difference in .NET Framework 2.0 or VS.Net 2005 compared to 1.1 and 2003.

Signature

Happy coding!
Morten Wennevik [C# MVP]

Sue & Bill - 23 Aug 2005 14:53 GMT
Thanks Morten.

I got it to work by casting DayOfWeek to (int), adding 6 to it, modulo
7 the sum, and recasting it to Day.

It works fine, but is it good practice to make assumptions about the
underlying value of eumerated types?  If Microsoft were to change the
integer values, my program would break.

These two properties are to get/set the day that is used as the first
day of the week in the current culture (eg Saturday is the first day of
the week for Saudi Arabia).  I wonder if it is a simple case of the
Windows.Forms department in Microsoft not talking to the Globalization
department.  There should be just one type for the two properties.
Morten Wennevik - 23 Aug 2005 15:17 GMT
I don't think either of the enumerations will ever change as it most likely will break a great many applications, but if you want to be sure any future changes won't break your code, you could always use the string value of the DayOfWeek enumeration and parse that with the Day enumeration.

DayOfWeek d1 = DayOfWeek.Sunday;
Day d2 = (Day)Enum.Parse(typeof(Day), d1.ToString());

> Thanks Morten.
>
[quoted text clipped - 10 lines]
> Windows.Forms department in Microsoft not talking to the Globalization
> department.  There should be just one type for the two properties.

Signature

Happy coding!
Morten Wennevik [C# MVP]


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.