O'Reilly logo

Visual Basic 2008 Recipes: A Problem-Solution Approach by Rakesh Rajan, Matthew MacDonald, Allen Jones, Todd Herman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

14.6. Implement a Type That Can Be Formatted

Problem

You need to implement a type that can create different string representations of its content based on the use of format specifiers for use in formatted strings.

Solution

Implement the System.IFormattable interface.

How It Works

The following code fragment demonstrates the use of format specifiers in the WriteLine method of the System.Console class. The codes in the braces (emphasized in the example) are the format specifiers.

Dim a As Double = 345678.5678
Dim b As UInteger = 12000
Dim c As Byte = 254

Console.WriteLine("a = {0}, b = {1}, and c = {2}", a, b, c)
Console.WriteLine("a = {0:c0}, b = {1:n4}, and c = {2,10:×5}", a, b, c)

When run on a machine configured with English (United States) ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required