This is a code snippet I use for an example of implementing IFormattable
and ICustomFormatter
.
Implements IFormattable
Implements ICustomFormatter
Public Function Format(ByVal formatExpression As String, ByVal arg As Object, ByVal formatProvider As System.IFormatProvider) As String Implements System.ICustomFormatter.Format
'type is currently ignored
' if type is international then "USPS" should result in international address
' if type is international then "US" should result in international address
' and so on
'
'.NET Framework Class Library
'IFormattable Interface
'Remarks - A class that implements IFormattable must support the "G" (general) formatting code. Besides the "G" code, the class can define the list of formatting codes that it supports.
'is G and g the same?
' yes for numeric
' no for date/time
'Standard Numeric Format Strings
' G or g - both are the same
'Standard DateTime Format Strings
' g - General date/time pattern (short time)
' G - General date/time pattern (long time)
If Len(formatExpression) = 0 Then
Return String.Format("{0}", arg)
End If
'usps - standardized
'us - address less country
'international - all address lines
If formatExpression.Equals("g") Then
'general formatting code
' as per documentation
Return GatherAddress(_line1, _line2, _city, _state, _zip, _country, _type, AddressFormat.StandardUS)
ElseIf formatExpression.Equals("G") Then
'general formatting code
' as per documentation
Return GatherAddress(_line1, _line2, _city, _state, _zip, _country, _type, AddressFormat.Standardized)
ElseIf formatExpression.ToUpper.Equals("USPS") Then
Return GatherAddress(_line1, _line2, _city, _state, _zip, _country, _type, AddressFormat.Standardized)
ElseIf formatExpression.ToUpper.Equals("US") Then
Return GatherAddress(_line1, _line2, _city, _state, _zip, _country, _type, AddressFormat.StandardUS)
ElseIf formatExpression.ToUpper.Equals("INTERNATIONAL") Then
Return GatherAddress(_line1, _line2, _city, _state, _zip, _country, _type, AddressFormat.International)
Else
Return MyBase.ToString()
End If
End Function
Public Overloads Function ToString(ByVal format As String, ByVal formatProvider As System.IFormatProvider) As String Implements System.IFormattable.ToString
Return Me.Format(format, Nothing, formatProvider)
End Function