Custom classes, ToString, and format strings

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nathan Sokalski

    Custom classes, ToString, and format strings

    I have a custom class, and I would like to overload the ToString method so
    that it accepts a format string like DateTime (and many other classes). I
    believe this involves implementing the IFormattable interface, but the stuff
    I found in the documentation showed it as accepting two parameters, the
    format string and a System.IFormatP rovider (I only want it to accept the
    format string). Does anybody know of any good examples of doing this? I am
    using VB.NET 2.0, but if an example in VB.NET is unavailable I would
    appreciate an example in C# as well. Thanks.
    --
    Nathan Sokalski
    njsokalski@hotm ail.com
    有声小说网为广大读者提供热门小说在线免费阅读,本站收集的网络文学小说情节跌宕起伏,有声小说网是值得书友们收藏的小说在线阅读网。



  • Steve Gerrard

    #2
    Re: Custom classes, ToString, and format strings

    Nathan Sokalski wrote:
    I have a custom class, and I would like to overload the ToString
    method so that it accepts a format string like DateTime (and many
    other classes). I believe this involves implementing the IFormattable
    interface, but the stuff I found in the documentation showed it as
    accepting two parameters, the format string and a
    System.IFormatP rovider (I only want it to accept the format string).
    Does anybody know of any good examples of doing this? I am using
    VB.NET 2.0, but if an example in VB.NET is unavailable I would
    appreciate an example in C# as well. Thanks.
    I think you can just do

    Public Overloads Function ToString(ByVal format As String) As String
    ' return a formatted string here
    End Function


    Comment

    • Cor Ligthert[MVP]

      #3
      Re: Custom classes, ToString, and format strings

      Nathan,

      Strange as there is a bunch of information on MSDN, one of those

      Learn how to convert instances of .NET types to formatted strings. Override the ToString method, make formatting culture-sensitive, and use ICustomFormatter.


      This one containts a lot of links,

      Cor

      "Nathan Sokalski" <njsokalski@hot mail.comschreef in bericht
      news:ObWu3RRyIH A.2188@TK2MSFTN GP04.phx.gbl...
      >I have a custom class, and I would like to overload the ToString method so
      >that it accepts a format string like DateTime (and many other classes). I
      >believe this involves implementing the IFormattable interface, but the
      >stuff I found in the documentation showed it as accepting two parameters,
      >the format string and a System.IFormatP rovider (I only want it to accept
      >the format string). Does anybody know of any good examples of doing this? I
      >am using VB.NET 2.0, but if an example in VB.NET is unavailable I would
      >appreciate an example in C# as well. Thanks.
      --
      Nathan Sokalski
      njsokalski@hotm ail.com
      有声小说网为广大读者提供热门小说在线免费阅读,本站收集的网络文学小说情节跌宕起伏,有声小说网是值得书友们收藏的小说在线阅读网。

      >

      Comment

      Working...