Subscribe to The Madman Scribblings        RSS Feed
-----

Custom FormatProviders

Icon Leave Comment
A couple of format providers.

The first is one the formats a boolean value, into either;
0 or 1 with the use of the format io
Yes or No with the use of the format yn

Public Class BoolFormatProvider
  Implements IFormatProvider, ICustomFormatter
#Region "ICustomFormatter Members"
  Public Function Format(ByVal formatt As String, ByVal arg As Object, ByVal formatProvider As IFormatProvider) As String Implements ICustomFormatter.Format
    Dim value As Boolean = CBool(arg)
    formatt = (If(formatt Is Nothing, Nothing, formatt.Trim().ToLower()))
    Select Case formatt
      Case "io" : Return If(value, "1", "0")
      Case "yn" : Return If(value, "Yes", "No")
      Case Else
        If TypeOf arg Is IFormattable Then
          Return DirectCast(arg, IFormattable).ToString(formatt, formatProvider)
        Else
          Return arg.ToString()
        End If
    End Select
  End Function
#End Region
#Region "IFormatProvider Members"
  Public Function GetFormat(ByVal formatType As Type) As Object Implements IFormatProvider.GetFormat
    If formatType = GetType(ICustomFormatter) Then Return Me
    Return Nothing
  End Function
#End Region
End Class



Public Module ext
  <Runtime.CompilerServices.Extension()>
  Public Function ToString(ByVal value As Boolean, ByVal format As String)
    Return String.Format(New BoolFormatProvider(), "{0:" + format + "}", value)
  End Function
End Module


The second format provider formats a long in a value as spoken.
Imports System.Globalization

Public Class LongAsWordsFP
  Implements IFormatProvider, ICustomFormatter

#Region "ICustomFormatter Members"
  Public Function Format(ByVal fmt As String, ByVal arg As Object, ByVal formatProvider As IFormatProvider) As String Implements ICustomFormatter.Format
    Dim value As Long = CType(arg, Long)
    fmt = If(fmt Is Nothing, Nothing, fmt.Trim().ToLower())
    Try
      Select Case fmt
        Case "words"
          Return WriteNumber(value)
        Case Else
          Return HandleOtherFormats(fmt, arg)
      End Select
    Catch e As FormatException
      Throw New FormatException(String.Format("The format of '{0}' is invalid.", fmt), e)
    End Try

  End Function
#End Region
  Private Function HandleOtherFormats(ByVal fmt As String, ByVal arg As Object) As String
    If TypeOf arg Is IFormattable Then
      Return DirectCast(arg, IFormattable).ToString(fmt, CultureInfo.CurrentCulture)
    ElseIf arg IsNot Nothing Then
      Return arg.ToString()
    Else
      Return String.Empty
    End If
  End Function
#Region "IFormatProvider Members"
  Public Function GetFormat(ByVal formatType As Type) As Object Implements IFormatProvider.GetFormat
    If formatType = GetType(ICustomFormatter) Then Return Me
    Return Nothing
  End Function
#End Region


End Class



 <Runtime.CompilerServices.Extension()>
  Public Function AsWords(ByVal Value As Long) As String
    Return String.Format(New LongAsWordsFP(), "{0:words}", Value)
  End Function

0 Comments On This Entry

 

Search My Blog

Recent Entries

Recent Comments