System.Windows.Form.Day vs System.DayOfWeek

  • Thread starter Thread starter Sue & Bill
  • Start date Start date
S

Sue & Bill

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
 
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.
 
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.
 
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());
 
Back
Top