ToString()
End Function
End Structure
Public Class EntryPoint
Shared Sub Main()
Dim local As CultureInfo = CultureInfo.CurrentCulture
Dim germany As CultureInfo = New CultureInfo("de-DE")
Dim cpx As Complex = New Complex(12.3456, 1234.56)
Dim strCpx As String = cpx.ToString("F", local)
CHAPTER 9 n WORKING WITH STRINGS 176
Console.WriteLine(strCpx)
strCpx = cpx.ToString("F", germany)
Console.WriteLine(strCpx)
Console.WriteLine("" & vbCrLf & "Debugging output:" & vbCrLf & _
"{0:DBG}", cpx)
End Sub
End Class
This is the output from the previous example:
( 12.35 : 1234.56 )
( 12,35 : 1234,56 )
Debugging output:
Strings1.Complex
real: 12.3456
imaginary: 1234.56
The real meat of this example lies within the implementation of IFormattable.ToString().
You implement a "DBG" format string for this type that will create a string that shows the internal
state of the object and may be useful for debug purposes. If the format string is not equal to
"DBG", then you simply defer to the IFormattable implementation of System.Double. Notice the
use of StringBuilder to create the string that is eventually returned. Also, we chose to use the
Console.WriteLine method and its format item syntax to send the debugging output to the
console just to show a little variety in usage.
Pages:
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299