Welcome to Strong Coders Community Sign in | Join | Help

Blog ( Of Ryan Olshan)

News

  • About Ryan Olshan

    Join StrongTypes - Free Generics Help By Email
    Join StrongTypes

    Community Credit Winner

    View my MVP profile

    The posts on this weblog are provided "as is" with no warranties and confer no rights. The opinions expressed herin are my own personal opinions and do not represent my employer's view in any way.

    Subscribe to Rolog via My AOL
    Subscribe to Rolog via Bloglines


    Subscribe to Rolog via Feedburner

    Furl Rolog




    Subscribe to Rolog via Newsburst


    Subscribe to Rolog via Pluck

    Add Rolog to Technorati Favorites




    Thycotic Secret Server

.NET Websites

  • => StrongTypes.com <=
  • Community Credit
  • DotNetCharity.com
  • DotNetSlackers
  • EasyListBox
  • SystemNetMail.com
  • SystemWebMail.com

Blogroll

  • Alex Campbell
  • Carlos Magalhaes
  • Chris Taylor
  • Darren Neimke
  • David Betz
  • Eric Smith
  • Rob Caron
  • Rob Howard
  • Sam Gentile
  • Scott Guthrie
  • Scott Mitchell
  • Tess
  • Wesner Moise
  • Jason Alexander
  • Bill Ryan
  • Steve Smith
  • Sahil Malik
  • David Silverlight

Other Websites

  • 911 Pigeon Alert
  • Pacific Wildlife Project

Strong Coder Bloggers

  • Dean Fiala
  • Kevin Jones

Thycotic Bloggers

  • Bryant Smith
  • John Morales
  • Jonathan Cogley

Yahoo! Groups

  • => StrongTypes <=
  • AspNet2
  • AspNet20GridView
  • PayPalDevDotNet
  • RubyOnRails
  • VSTS

A somewhat type-safe String.Format

Using String.Format you can format any string. However, the last parameter that String.Format accepts is of type object. So, to add some type-safety to the mix, the following function works perfectly.

VB.NET
Public Function FormatString( Of T)( ByVal originalString As String , ByVal replacement As T) As String
    Return String .Format(originalString, replacement)
End Function

C#
public string FormatString( string originalString, T replacement)
{
    return String .Format(originalString, replacement);
}

Share this post: | bookmark it! | digg it! | reddit! | kick it! | live it!
Posted: Monday, January 23, 2006 12:50 PMby Ryan Olshan
Filed under:

Comments

Marc Brooks said:

What does this buy you?

It does insure that you pass an integer when you doe FormatString("d", 2), but you still have no linkage between the formatting tokens and the replacement object, which is really where the type-safety would be useful (like I should have given a DateTime here). It's not that useful in the other major use of String.Format, for parameter rearrangement either.
# January 23, 2006 3:49 PM

Ryan Olshan said:

That's why I named this blog entry "A somewhat type-safe String.Format."

It does add some type-safety on what you are passing to it. However, there isn't any type-safety on what happens when the string is formatted. The function does limit it to only one of the overloads of String.Format, but you could add other overloads. While it's not the best example as a use for generics, it should hopefully get people not familiar with or confused about generics thinking as to how they can apply generics.
# January 23, 2006 6:02 PM
New Comments to this post are disabled