First time here? Check out the site's "greatest hits" or read a post from the archives. Feel free to leave a comment or ask a question, and consider subscribing to the latest posts via RSS or e-mail. Thanks for visiting!
« Work continues on dasBlog... | Main | Now THERE'S a Training Session I'd like ... »

Well, here you go, it's my implementation of a FormattableObject that I mentioned earlier. Take a look at the comments, Mark Miller had some great ideas, the best of which being a CodeRush plugin that handles the static analysis at design time and makes it harder to mess up these strings.

You can do this:

[Test]

public void MakePersonFormattedStringWithFormat()

{

    Person p = new Person();

    string foo = p.ToString("{BirthDate:D} My name is {FirstName} {LastName} and I'm cool.");

    Assert.AreEqual("Tuesday, January 22, 1974 My name is Scott Hanselman and I'm cool.", foo);

}

or

[Test]

public void MakePersonFormattedStringWithFormatAndChineseCulture()

{

    Person p = new Person();

    string foo = p.ToString("{BirthDate:D} My name is {FirstName} {LastName} and I'm cool.",new System.Globalization.CultureInfo("zh-cn"));

    Assert.AreEqual("1974?1?22? My name is Scott Hanselman and I'm cool.", foo);

}

You can either:

  • Derive your objects from FormattableObject and you get these ToString implementations for free

or

  • Implement a few ToString's and IFormattable yourself and delegate over to the static implementations in FormattableObject:

 
public string ToString(string format, System.IFormatProvider formatProvider)

{

    return Corillian.Voyager.Common.ObjectFormatString.ToString(this,format,formatProvider);

}

 

public string ToString(string format)

{

    return this.ToString(format,null);

}

Here you go, enjoy.  If you don't, screw you.  If you do, I take full credit.

FormattableObject 0.5 (11kb)

Tracked by:
"ObjectToString()" (VBASPCoder.com(日本語版)) [Trackback]
"The DebuggerDisplayAttribute in Visual Studio 2005 and IFormattable" (ComputerZ... [Trackback]
" Aggregated reflection-based ToString() implementation" (.Net Adventures ) [Trackback]
http://ianwhite.is-a-geek.net/PermaLink.aspx?guid=68ae51dd-d60a-48ea-9cd6-ac0e08... [Pingback]
"Better String.Format" (AGBlog) [Trackback]


Monday, January 31, 2005 12:22:22 AM (Pacific Standard Time, UTC-08:00)
Yes, it seems to work well with Japanese characters (environment) as well. I updated my blog entry after trying your code out.

http://www.vbaspcoder.com/PermaLink,guid,13fe24f3-7129-4b42-8d0b-253d8e829f60.aspx
Wednesday, February 02, 2005 5:48:57 AM (Pacific Standard Time, UTC-08:00)
It can be cleaner...
Instead of manually building the result string, you could use:

reg.Replace(aFormat, new MatchEvaluator(Evaluate));

The regex would be (no IgnoreCase needed): @"{([^}]+)}"

And:

string Evaluate(Match match)
{
string property = match.Groups[1].Value;
...
}
Diego Mijelshon
Thursday, February 24, 2005 2:48:37 PM (Pacific Standard Time, UTC-08:00)
Very nice!
I took your idea and slapped together a version that runs on datasets. Completely different implementation, nowhere near as spiffy as using reflection, but still with the human readable string goodness. It's at http://www.aigee.org/agblog/archives/762-Better-String.Format.html if anyone is interested. Try not to laugh too hard at the coded-in-5-minutes sloppy code :)
Comments are closed.

Contact

Sponsors

Hosting By

On this page...

Tags

Calendar

<November 2008>
SunMonTueWedThuFriSat
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456

Archives

Google Ads