Archive for September 2006

IFormatProvider and NumberFormatInfo nightmare

September 15, 2006

I was implementing FormatProvider to support a special unit of measure called basis points (bp) which basically means 1/10000.

class MyFormatInfo: IFormatProvider, ICustomFormatter
{
   #region IFormatProvider Members
   public object GetFormat(
Type formatType)
   {
      return this;
   }
   #endregion

   #region ICustomFormatter Members
   public string Format(string format, object arg,
IFormatProvider formatProvider)
   {
      string aLowerFormat = format.ToLowerInvariant();
      switch (aLowerFormat)
      {
         case
“bp”:
            return FormatNumeric(arg.ToString(), 10000);
         case
“pc”:
            return FormatNumeric(arg.ToString(), 100);
      }
      return string.Format(
CultureInfo.CurrentCulture, format, arg);
   }
 

   private static string FormatNumeric(string theNumber, decimal theMultiplier)
   {
      decimal aNumericValue;
      if (decimal.TryParse(theNumber, out aNumericValue))
      {
         aNumericValue *= theMultiplier;
         return aNumericValue.ToString(
“N”, CultureInfo.CurrentCulture);
      }
      else

      {
         return string.Empty;
      }
   } 
   #endregion

}

This works great when used with string.Format such as string.Format(“{0:bp}”, 0.01).

However, once it is being used with .ToString(new MyFormatProvider()), nothing works. It didn’t even hit the Format() method. The ToString(IFormatProvider) method supposedly works with custom format provider. (Unless why would it even accept IFormatProvider)

You might say, “Then just use the damn string.Format method.” I know, I know, but how would you bind your object to a datagrid and have the decimal or double value be displayed in the custom format?

grid.CellStyle.FormatProvider = new MyFormatProvider();
grid.CellStyle.Format = “bp”;

A trip to Reflector reveals the ugly implementation of decimal.ToString(IFormatProvider) and double.ToString(IFormatProvider). The truth is, they will check if the IFormatProvider is actually a NumberFormatInfo. If it’s not, then it will ignore everything and use the NumberFormatInfo from the CurrentCulture.

I don’t really understand why it was implemented that way, but okay Microsoft, how about I subclass from NumberFormatInfo. Then my format provider will be NumberFormatInfo, right? Right, except for one small detail: NumberFormatInfo is a sealed class. Bang…I hit my head against the wall again.

Then, my last resort is try to find standard format hack and lucky me, I found out that the format ‘%’ is doing no more than multiply the original number with 100 just like how the ‘,’ format is dividing by 100. So, here goes it.

NumberFormatInfo aCustomFormat = (NumberFormatInfo)

CultureInfo.CurrentCulture.NumberFormat.Clone();

aCustomFormat.PercentSymbol = “”;

decimal x = 0.0125M;

Console.WriteLine(x.ToString(“0.##%\%”, aCustomFormat));    // 1.25%

Console.WriteLine(x.ToString(“0.##%%bp”, aCustomFormat));   // 125bp

I was lucky this time, but if my requirements cannot be met by tricking ‘%’ or ‘,’, then I will be in trouble. Hopefully somebody will fix the ToString(IFormatProvider) implementation in the future or explain why it should be implemented this way.

Advertisements

Eclipse to TFS Integration

September 12, 2006

Looks like this has become quite popular.

Commercial vendors that provide Eclipse plug-in are Teamprise and Teamplain.

 An open source alternative that has just started earlier this year is at VSTS Eclipse.

CodeCamp coming to Silicon Valley, October 7-8

September 11, 2006

Finally, we are going to have a CodeCamp. Woohoo…. Peter Kellner has been working on setting one up.

When : October 7-8, 2006
Where : Computers Technology and Information Systems, Foothill College, 12345 El Monte Road, Los Altos Hills, CA 94022
How: Register at Silicon Valley CodeCamp site.

See you there….

Scrum for VSTS

September 11, 2006

Scrum for Team System is free Agile Software Development Methodology add-in for Microsoft Visual Studio Team System, developed by Conchango, in collaboration with Ken Schwaber, co-creator of Scrum, and the Microsoft Technology Centre UK: http://www.scrumforteamsystem.com

TeamPlain for VSTS

September 11, 2006

Looks like TeamPlain by devBiz are doing a great job at extending VSTS as a framework for life cycle application. So far, they have a decent looking web UI for TFS that should have been part of TFS all along. The Eclipse plug-in looks awesome, too.

Their website: http://www.devbiz.com/teamplain/

Their development blog: http://dev.devbiz.com/blogs/teamplain/

Comparing TFVC and CVS

September 11, 2006

I have always thought that CVS is where all SCC got the idea from, but I guess not. My favorite SCC feature is Changelist/Changeset and looks like only Perforce, Subversion and TFVC got it.

For a comparison list: http://blogs.msdn.com/buckh/archive/2005/06/03/425125.aspx