Scott Hanselman

RELEASE: FormattableObject an Aggregating reflection-based ToString() implementation

January 31, 2005 Comment on this post [3] Posted in ASP.NET | Internationalization | CodeRush
Sponsored By

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)

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook bluesky subscribe
About   Newsletter
Hosting By
Hosted on Linux using .NET in an Azure App Service

Work continues on dasBlog...

January 30, 2005 Comment on this post [4] Posted in ASP.NET | DasBlog | Bugs
Sponsored By

The team's work continues on dasBlog. You can see the moment to moment details in the Bugs or Tracker section of SF.

If that's too much data, there's a roll up on the Wiki that shows the bugs, new features and goodness that'll be in an upcoming "point release" of 1.7 if you've been wisely hanging back while we tighten the screws.

Here's some highlights:

  • Admin: UploadImage and UploadAttachment have returned in their previous incarnations. We also include the Image Gallery (which some people dig immensely) and support for an RSS Enclosure.
  • Macros: We support two new macros for "Compressed Titles" that can allow dasBlog users to completely remove the look of "permalink.aspx" in lieu of something like: http://www.hanselman.com/blog/WorkContinuesOnDasBlog.aspx. Some tweaks and controversy remain. We shall see.
  • Small Breaking Change: The TitleList macro will require parens to be called, ala TitleList().
  • And lots of small bug fixes.

More soon.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook bluesky subscribe
About   Newsletter
Hosting By
Hosted on Linux using .NET in an Azure App Service

Is this a good idea? Has it been done? Should it? An Aggregating ToString() implementation...

January 28, 2005 Comment on this post [14] Posted in ASP.NET | NUnit
Sponsored By

Travis and I were kicking around this idea. It's either already been done and it's a great idea, it's a good idea and no one has bothered, or it's stupid because _______.

We use a lot of "Domain Objects" like say, "public class Person." Why not have a "aggregated" ToString override like this:

Person p = new Person("Scott","Hanselman",new DateTime(1974,1,22);
//blah blah blah
string foo = p.ToString("My name is {FirstName} {LastName} and my birthday is {Birthdate:MM/dd/yyyy}");

Is this stupid? Would it gain you anything over:

string foo = String.Format("My name is {0} {1} and my birthday is {2:MM/dd/yyyy}",p.FirstName,p.LastName,p.BirthDate);

I've got it half done, but I wanted to know your thoughts before I finish it.

My thinking is that, even though it'd be slow (Reflection) it's useful for these reasons:

  • Quick Testing - Seems convenient to me, useful for NUnit.
  • Ultra-Late Bound - Ya, I know I'm Mr. So-Early-Bound-I-Generate-Everything but sometimes you just don't know until late, which leads me to:
  • Externalization of Complex Formatted Strings - True, you're embedding knowledge of your properties in a string, but it'd allow you to add a different series of fields if another language required it. This would be an improvement over ordinal style ({0}, {1}) format strings, no? Perhaps a silly use case.
  • DataBinding - You can let an ASP.NET DataGrid just call ToString on an object and it'll "do the right thing" as opposed to doing a TemplateColumn or an OnItemDataBound callback.
  • It just calls down into the underlying object's ToString - It's basically using the aggregate ToString's format string to get Properties and Fields and call their respective ToString's using the embedded (after the colon) format string.

Am I smoking crack?

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook bluesky subscribe
About   Newsletter
Hosting By
Hosted on Linux using .NET in an Azure App Service

Turning off AutoComplete for TextBoxes in IE and FireFox

January 27, 2005 Comment on this post [6] Posted in ASP.NET
Sponsored By

This is one a lot of people know, but it's worth covering again because it's easily forgotten as it's a small detail. Since we do eFinance sites, we often don't want folks' UserNames collected and stored in AutoComplete, especially when the site is browsed on a public machine.

<form id="formSignOn" autocomplete="off" method="post" runat="server">

Note that autocomplete="false" doesn't work. However, autocomplete="off" works in both IE and FireFox.

Thanks to Johnson Michael for reminding me of this tip.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook bluesky subscribe
About   Newsletter
Hosting By
Hosted on Linux using .NET in an Azure App Service

Internet access (and Skype) at 36000ft. Schweet.

January 26, 2005 Comment on this post [6] Posted in Musings
Sponsored By

Clemens is such a stud. He chatted me last night around 8:20pm PST and was flying SAS to Copenhagen. Apparently for $30 you can get internet access while on the flight. OMG. That would so make a long-ass trip easier.

If you look REALLY close at his laptop screen, you can see my Tiny Wedding Picture (you remember that one) in the corner of MSN Messenger.

He tried to Skype me as well, but either the bandwidth wasn't up to snuff or the roar of the plane was too much for the noise cancelling.

Interestingly, I could hear him typing, and he I, but we couldn't understand speech. Either way, I hope this becomes standard fare. It all done by bouncing off sattellites. I would love to run some TraceRTs on that connection.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook bluesky subscribe
About   Newsletter
Hosting By
Hosted on Linux using .NET in an Azure App Service

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.