Why Can’t I Use My Own Calendar Classes In Custom Cultures ?

In Tuesday’s webcast on Custom Cultures I was asked a very interesting questions about creating a Hindi Calendar for use with a custom culture. My answer was a bit misleading because I was focussing on the problem of how to create a calendar which had quite different rules to most other calendars and I forgot the simple truth which is that you can’t use custom calendars in custom cultures. Here’s why.

The Problem


It is possible to create your own Calendar classes by inheriting from the Calendar class and then implementing all of the Calendar classes’ abstract members. The problem is that when you attempt to assign this new Calendar to the DateTimeFormatInfo.Calendar property an InvalidOperationException is thrown:-

DateTimeFormatInfo dateTimeFormatInfo = new DateTimeFormatInfo();

dateTimeFormatInfo.Calendar = new NewCalendar();

The InvalidOperationException message is "Cannot set sub-classed Globalization.NewCalendar object to System.Globalization.DateTimeFormatInfo object.".

The Explanation


The DateTimeFormatInfo.Calendar property setter block includes a "domain safety check". This check tests that the Calendar object being assigned to the Calendar property comes from the same assembly as the System.Globalization.CultureInfo class (i.e. mscorlib.dll). So if your new Calendar class does not originate from mscorlib.dll then an exception will be thrown. The problem is that only Microsoft can make changes to mscorlib.dll. Consequently there is no way that your new Calendar class can be used with DateTimeFormatInfo objects and therefore no way that you can use your own Calendar classes in custom cultures.

As an aside you might consider assigning the new Calendar object to the CultureAndRegionInfoBuilder.AvailableCalendars property:-

builder.AvailableCalendars = new Calendar[] { new NewCalendar() };

This assignment results in a NotSupportedException with the message "Custom calendars are not currently supported.". The message gives a clear indication that this feature is not yet available in the .NET Framework 2.0 but that it might become available at some future date.

Currently rated 4.5 by 2 people

  • Currently 4.5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted by: Guy Smith-Ferrier
Posted on: Saturday, July 15, 2006 at 1:00 AM
Categories: Internationalization
Actions: E-mail | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (0) | Post RSSRSS comment feed

"Creating Custom Cultures in the .NET Framework 2.0" MSDN Webcast Available For Download

If you missed Tuesday’s "Creating Custom Cultures in the .NET Framework 2.0" MSDN Webcast you can now download it here. The source code mentioned in the webcast can be downloaded here. Chapter 11 of .NET Internationalization, Creating Custom Cultures, can be downloaded here.

Enjoy.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted by: Guy Smith-Ferrier
Posted on: Friday, July 14, 2006 at 1:00 AM
Categories: Events
Actions: E-mail | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (4) | Post RSSRSS comment feed

Brian Long’s .NET Debugging Slides

Many thanks to Brian Long for last night’s presentation on .NET Debugging - another great performance. You can download Brian’s slides here.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted by: Guy Smith-Ferrier
Posted on: Thursday, July 13, 2006 at 1:00 AM
Categories: Events
Actions: E-mail | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (2) | Post RSSRSS comment feed

Ding Dong The Witch Is Dead

The 11th July 2006 saw the end of Microsoft’s support for Windows 98, 98SE and ME. Putting all other arguments aside, in internationalization terms this is wonderful news. This means that there are no more non-Unicode desktop versions of Windows that are officially supported. All that effort that goes into supporting these inadequate operating systems can go away and we can hence forth focus solely on Unicode for delivering internationalized applications. Yesterday was indeed a happy day.

For more information see:-

http://blogs.pcworld.com/techlog/archives/002357.html

http://news.bbc.co.uk/1/hi/technology/5164450.stm

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted by: Guy Smith-Ferrier
Posted on: Wednesday, July 12, 2006 at 1:00 AM
Categories: Internationalization
Actions: E-mail | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (0) | Post RSSRSS comment feed

Final Reminder: Custom Cultures Webcast On Tuesday

Just a reminder that I will be presenting an MSDN webcast entitled "Creating Custom Cultures In The .NET Framework 2.0" tomorrow (Tuesday 11th July) at 5:00pm BST (9:00am Pacific Time). Click here to register.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted by: Guy Smith-Ferrier
Posted on: Monday, July 10, 2006 at 1:00 AM
Categories: Events
Actions: E-mail | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (0) | Post RSSRSS comment feed