Plunging into .NET Development

Weblog Pieter Gheysens
Microsoft .NET Development - C# - Enterprise Library - Visual Studio 2005 Team System - Compuware DevPartner - ...
 


Tuesday, July 31

Visual Studio 2008 Beta 2

It's always exciting when a new version of Visual Studio becomes available. A few days ago, Microsoft released Visual Studio 2008 Beta 2 and the .NET Framework 3.5 Beta 2. During the week-end I managed to download Beta 2 and I now also installed it properly.



One of the cool things with Visual Studio 2008 is that developers can target multiple versions of the .NET Framework in Visual Studio 2008. So, you can make use of the new IDE features while still targeting the .NET Framework 2.0.



Be sure to read Scott Guthrie's blogposts on the release of Visual Studio 2008. You need to be aware of certain issues and you can find a lot of extra information ...

Links :

Labels:

Sunday, July 29

custom Formatting with CultureInfo

The CultureInfo class in .NET provides information about a specific culture (also known as a locale). The information in the culture includes the names for the culture, the writing system, the calendar used, and formatting for dates, currency and numbers.

At start-up of an application you should always set the desired CultureInfo to the running thread to avoid wrong formatting of DatetTime and Number information in your application. Especially if you need custom formatting ... because by default, the running thread takes the culture settings of the machine it is running on. In the example below I set some custom fixed formatting options for DateTime and numbers. Afterwards I assign this cultureInfo to the CurrentCulture of the current thread.

    //create custom cultureInfo based on nl-be

    CultureInfo cultureInfo = new CultureInfo("nl-be");

    //DateTimeFormatting

    cultureInfo.DateTimeFormat.ShortDatePattern = "dd/MM/yyyy";

    cultureInfo.DateTimeFormat.ShortTimePattern = "HH:mm";

    //NumberFormatting

    cultureInfo.NumberFormat.NumberDecimalDigits = 2;

    cultureInfo.NumberFormat.NumberGroupSeparator = " ";

    cultureInfo.NumberFormat.NumberDecimalSeparator = ",";

 

    Thread.CurrentThread.CurrentCulture = cultureInfo;

 

    DateTime testDateTime = DateTime.UtcNow;

    decimal testDecimal = 123456.789012M;

 

    StringBuilder sbOutput = new StringBuilder();

    sbOutput.Append("Date : " + testDateTime.ToShortDateString());

    sbOutput.Append(Environment.NewLine);

    sbOutput.Append("Time : " + testDateTime.ToShortTimeString());

    sbOutput.Append(Environment.NewLine);

    sbOutput.Append("testDecimal : " + testDecimal.ToString("N"));

 

    MessageBox.Show(sbOutput.ToString());


As a result, the ToShortDateString method on the DateTime object will always display the DateTime information in the desired format. Also the number formatting for the decimal is like it should be. Note that I must pass the numeric format N to the ToString method so that the decimal will be shown as a number. More information about standard numeric format strings at MSDN.



If you need formatting that deviates from the default formatting specified in the current thread, you can still specify a custom FormatProvider in the ToString method.

    NumberFormatInfo nfi = new NumberFormatInfo();

    nfi.NumberDecimalDigits = 4;

    nfi.NumberGroupSeparator = ",";

    nfi.NumberDecimalSeparator = ".";

 

    decimal testDecimal = 123456.789012M;

    MessageBox.Show(testDecimal.ToString("N", nfi));




My advice : do not create your own custom formatting/conversion functions in your applications, but try to keep it simple and use the stuff that's already available in the .NET Framework (CultureInfo class)!

One extra remark on cultures on the CurrentThread :
  • CurrentUICulture specifies the culture for the user interface and is used for resource file lookups.
  • CurrentCulture specifies the culture for data and information. It is used to format dates, numbers, currencies and sort strings.

Labels:

Saturday, July 28

Dual boot problems - Vista?

I'm running a dual boot (Windows Server 2003 and Vista Ultimate) on my laptop for quite some time, but last week I did a re-install of Windows Server 2003. The first problem that came up was that the boot option for Vista disappeared. Vista was still installed on my hard drive, but the start-up option for Vista was gone. The first thing I tried to do was to recover the Vista boot with the recover option in the setup of the Vista install DVD. This worked out fine, except that I now lost the Windows Server 2003 boot option.

After some googling I found out that the Boot Loader for Vista has changed (no more boot.ini magic) ...

Windows Vista introduces a new boot loader architecture; a new firmware-independent boot configuration and storage system called Boot Configuration Data (BCD); and a new boot option editing tool, BCDEdit (BCDEdit.exe). These components are designed to load Windows more quickly and more securely.
A command-line tool (bcdedit.exe) is available in the system32 folder to modify the boot configuration data store. For those (like me) who are not familiar with the bcdedit.exe options and switches, you can download a freeware application (VistaBootPro) to easily and neatly organize the boot configuration entries from within all flavors of Windows. So I managed to add the Windows 2003 Server option back in my boot menu. Case closed!

Labels:

Tuesday, July 24

How to find switch-dates for daylight saving time?

After my previous post about the daylight saving time, someone asked me how he could dynamically retrieve the switch-dates for the daylight saving time in .NET ...

class Program

{

    static void Main(string[] args)

    {

        for (int i = 2007; i < 2010; i++)

        {

            DaylightTime daylightTime = TimeZone.CurrentTimeZone.GetDaylightChanges(i);

            Console.WriteLine("year : " + i.ToString());

            Console.WriteLine("- switch winter/summer : " + daylightTime.Start.ToString());

            Console.WriteLine("- switch summer/winter : " + daylightTime.End.ToString());

            Console.WriteLine("");

        }

 

        Console.ReadLine();

    }

}


Labels:

Saturday, July 21

Daylight saving time

Last week I got a headache from discussions about the daylight saving time and how to handle these transitions in software applications. To stop my frustration about this issue, I need some kind of closure and hopefully I can do this with writing down my point of view in this blogpost ...

Daylight saving time (DST) is the convention of advancing clocks so that afternoons have more daylight and mornings have less. Typically clocks are adjusted forward one hour near the start of spring and are adjusted backward in autumn.

DST (also called summer time) is common in Europe but this convention is not used everywhere in the world. The European Union shifts time zones all at once, at 01:00 Coordinated Universal Time (UTC). Belgium lies in the UTC/GMT +1 time zone. This means that the daylight saving time shift occurs at 02:00 local time. For 2007, the switch to summer time occurs on March 25, 02:00 local time : clocks are turned forward 1 hour to 03:00 local time. This means that the UTC offset is now +2 instead of +1. The switch to winter time in 2007 occurs on October 28, 03:00 local time : clocks are turned backward 1 hour to 02:00 local time. The UTC offset is set back to +1.

The problem in software applications is that you have days with 23 hours and days with 25 hours when using local time. If we take a closer look at a 25-hour day in Belgium, we can see that we have two runs of local hour 02:00 - one normal run and one run after the daylight saving time switch. If you store datetime information in local time, you won't be able to get the difference between the first run and the second run. Therefore, you should store all datetime information in UTC time instead of local time. UTC time makes sure that all days have 24 hours. Local time will only be used to display datetime information in your user interface and conversions will be made from local time to a UTC time and from UTC time to local time. These conversions are only required in the user interface and all other datetime information in the backend needs to be set in UTC time.

In .NET, a ToUniversalTime method exists on the DateTime object to convert a local time to UTC0 and a ToLocalTime method exists on the DateTime object to convert a UTC0 time to local time. So, if you don't want to make it more complex and don't want to write some extra conversion functions, you will agree that storing all datetime information needs to be done in UTC0 time. Right?! I don't want to go into details, but you may store datetime information in no matter what UTC time (never local time!), but UTC0 will be the best and most obvious choice. Manually adding/subtracting hours to DateTime objects in code to comply to a specific UTC time should be avoided at all cost. Always use the out-of-the-box conversions on the DateTime object. When your application has interfaces/contracts with other applications, you should try to agree on passing datetime information in UTC0 as well. If you start mixing different UTC times, you will notice that it becomes more difficult to smoothly maintain/convert all datetime information. In situations where other applications want to deliver information to you in UTCx time where x is not 0 (due to history or other bad/wrong reasons), you still want to convert all datetime information to UTC0 in your backend application. Of course : you don't have to believe me! Try it out for yourself and see how it works out ...

Wikipedia links :

Monday, July 16

TFS Version Control - pending changes

What to do if a team member leaves your team and still has pending changes on your project? Or, you suffer from a computer crash and you still had pending changes in your workspace on that computer? How to resolve those pending changes? Well, you will have lost those pending changes, but you can undo (unlock) those pending changes via the tf executable (%drive%\Program Files\Microsoft Visual Studio 8\Common 7\IDE). As a global admin you can unlock specific items in the version control repository or you can also execute a delete of a workspace of a particular user. To use the lock command, you must have the Lock permission and to modify or delete an existing workspace, you must be the owner or have the global Administer workspaces permission ...

More info at MSDN :

Monday, July 2

Community Day - VISUG - Presentation

Last Thursday (June 28, 2007) - on the Community Day, I gave a presentation about the Software Development Lifecycle with Visual Studio Team System. You can download the slides of the presentation here.

I only had 45 minutes for the presentation : 15 minutes for a quick overview about the Software Development Lifecycle and 30 minutes for a demo of Visual Studio Team System. I showed the demo on a virtual pc with the Orcas beta 1 version of Visual Studio Team System. It was really hard to get it all done in 45 minutes. Each specific topic I covered in the demo would actually suit for a dedicated 1 hour session ...

Labels: ,