In the case of 1), I'd think you could also add a say, 
.GregsPreferredFormatString property & associated instance var to store it in, 
couldn't you?

Could you do likewise with extension methods?  (That's what I'd do in ruby...)

Cheers,

-Roy

-----Original Message-----
From: Discussion of advanced .NET topics. [mailto:[EMAIL PROTECTED] On Behalf 
Of Marc Brooks
Sent: Wednesday, August 13, 2008 1:19 AM
To: ADVANCED-DOTNET@DISCUSS.DEVELOP.COM
Subject: Re: [ADVANCED-DOTNET] Can you extend the datatypes in VB.Net

> Thanks for the feedback.   I have looked at the IFormatProvider and it will 
> work for what I am doing.
> However, it's not the problem that I am having.

As I see it, you have two options:

1) Declare a value-type wrapper for each of the types that have different 
system-wide formatting requirements and override the
ToString() method to "do the right thing every time".  You can create implicit 
conversions to and from the underlying native type so that they can be freely 
passed around.

2) Declare a mapping of types to IFormatProviders (a Dictionary<Type,
IFormatProvider>) and initialize that at startup.  Then EVERYWHERE you
want the value formatted, you can call a helper method that takes the raw value 
and looks up the IFormatProvider to pass down to the native
ToString() methods.  In GMail tested code:

public static Dictionary<Type, IFormatProvider> s_Providers = /// init this up

public static string MyFormattedToString<T>(T value) {
   IFormatProvider formatter = s_Providers[typeof(T)];

   // no formatter registered, punt to general ToString();
   if (formatter == null)
      return value.ToString();

   // use the formatter as desired... and return
   return value.ToString(formatter);
}

--
"Your lack of planning DOES constitute an emergency on my part... so PLAN 
BETTER! "

Marc C. Brooks
http://musingmarc.blogspot.com

===================================
This list is hosted by DevelopMentor   http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

===================================
This list is hosted by DevelopMentor®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to