IFormatProvider.GetFormat Method
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Returns an object that provides formatting services for the specified type.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- formatType
- Type: System.Type
An object that specifies the type of format object to return.
Return Value
Type: System.ObjectAn instance of the object specified by formatType, if the IFormatProvider implementation can supply that type of object; otherwise, Nothing.
GetFormat is a callback method that formatting and parsing methods invoke to retrieve information about the format of the input string in parsing operations or the format of the output string in formatting operations. In the formatType parameter, the formatting or parsing method passes the type of object it requires to perform its operation. If the IFormatProvider implementation can supply this formatting or parsing object, it returns that object. If not, it returns Nothing.
For example, in the call to the Int32.ToString(IFormatProvider) method, the method argument is an IFormatProvider object that provides information about how the string representation of the current integer instance might be formatted. When the runtime executes the method, it calls the IFormatProvider object's GetFormat method and passes it a Type object that represents the NumberFormatInfo type. If the IFormatProvider object can supply a NumberFormatInfo object, it returns that object. If it cannot supply an object of that type, it returns Nothing.
You can implement the IFormatProvider interface and the GetFormat method in classes that provide custom formatting or parsing services. The IFormatProvider implementation is then passed as an argument to any overload of a parsing or formatting method that has a parameter of type IFormatProvider, such as String.Format(IFormatProvider, String, Object()), Int32.ToString(String, IFormatProvider), or DateTime.Parse(String, IFormatProvider).
The following example illustrates the use of a class that implements the IFormatProvider interface and the GetFormat method. The AcctNumberFormat class converts an Int64 value that represents an account number to a formatted 12-digit account number. Its GetFormat method returns a reference to itself if the formatType parameter refers to a class that implements ICustomFormatter; otherwise, GetFormat returns Nothing.
Public Class AcctNumberFormat : Implements IFormatProvider, ICustomFormatter Private Const ACCT_LENGTH As Integer = 12 Public Function GetFormat(ByVal formatType As Type) As Object _ Implements IFormatProvider.GetFormat If formatType Is GetType(ICustomFormatter) Then Return Me Else Return Nothing End If End Function Public Function Format(fmt As String, arg As Object, formatProvider As IFormatProvider) As String _ Implements ICustomFormatter.Format ' Provide default formatting if arg is not an Int64. If Not TypeOf arg Is Int64 Then Try Return HandleOtherFormats(fmt, arg) Catch e As FormatException Throw New FormatException(String.Format("The format of '{0}' is invalid.", fmt), e) End Try End If ' Provider default formatting for unsupported format strings. fmt = fmt.ToUpper(CultureInfo.InvariantCulture) If Not (fmt = "H" Or fmt = "I") Then Return HandleOtherFormats(fmt, arg) End If ' Convert argument to a string. Dim result As String = arg.ToString() ' If account number is less than 12 characters, pad with leading zeroes. If result.Length < ACCT_LENGTH Then result = result.PadLeft(ACCT_LENGTH, "0"c) ' If account number is more than 12 characters, truncate to 12 characters. If result.Length > ACCT_LENGTH Then result = Left(result, ACCT_LENGTH) If fmt = "I" ' Integer-only format. Return result ' Add hyphens for H format specifier. Else If fmt.ToUpper = "H" ' Hypenated format. Return Left(result, 5) & "-" & Mid(result, 6, 3) & "-" & Right(result, 4) ' This branch should never be reached. Else Try Return HandleOtherFormats(fmt, arg) Catch e As FormatException Throw New FormatException(String.Format("The format of '{0}' is invalid.", fmt), e) End Try End If End Function Private Function HandleOtherFormats(fmt As String, 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 End Class
An instance of the AcctNumberFormat class can then be passed as an argument to a method that provides formatting or parsing services. For example, the following code passes an AcctNumberFormat class to the String.Format(IFormatProvider, String, Object()) method to generate a formatted 12-digit account number.
Imports System.Globalization Public Enum DaysOfWeek As Long Monday = 1 Tuesday = 2 End Enum Module Example Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock) Dim acctNumber As Long, balance As Double Dim wday As DaysOfWeek Dim output As String acctNumber = 104254567890 balance = 16.34 wday = DaysOfWeek.Monday output = String.Format(New AcctNumberFormat(), "On {2}, the balance of account {0:H} was {1:C2}.", acctNumber, balance, wday) outputBlock.Text += output + vbCrLf wday = DaysOfWeek.Tuesday output = String.Format(New AcctNumberFormat(), "On {2}, the balance of account {0:I} was {1:C2}.", acctNumber, balance, wday) outputBlock.Text += output + vbCrLf End Sub End Module ' The example displays the following output: ' On Monday, the balance of account 10425-456-7890 was $16.34. ' On Tuesday, the balance of account 104254567890 was $16.34.