IFormatProvider.GetFormat
If formatType Is GetType(ICustomFormatter) Then
Return Me
Else
Return CultureInfo.CurrentCulture.GetFormat(formatType)
End If
End Function
'ICustomFormatter implementation
Public Function Format(ByVal formatString As String, ByVal arg As Object, _
ByVal formatProvider As IFormatProvider) As String _
Implements System.ICustomFormatter.Format
If TypeOf arg Is IFormattable AndAlso formatString = "DBG" Then
Dim cpx As Complex = DirectCast(arg, Complex)
'Generate debugging output for this object.
Dim sb As StringBuilder = New StringBuilder()
sb.Append(arg.[GetType]().ToString() & "" & vbLf & "")
sb.AppendFormat("" & vbTab & "real:" & vbTab & "{0}" & _
vbLf & "", cpx.Real)
sb.AppendFormat("" & vbTab & "imaginary:" & vbTab & "{0}" & _
vbLf & "", cpx.Img)
Return sb.ToString()
Else
Dim formattable As IFormattable = TryCast(arg, IFormattable)
If formattable Is Nothing Then
Return formattable.ToString(formatString, formatProvider)
Else
Return arg.ToString()
End If
End If
End Function
End Class
Public Structure Complex
Implements IFormattable
CHAPTER 9 n WORKING WITH STRINGS 178
Private mReal As Double
Private mImaginary As Double
Public Sub New(ByVal real As Double, ByVal imaginary As Double)
Me.
Pages:
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301