1. Home
  2. Computing & Technology
  3. Visual Basic

Formatting Strings, Numbers, and Other Objects
The Format Method in VB.NET

By Dan Mabbutt, About.com

Sep 11 2009

VB.NET offers more! To start, there is also the Format method which is not at all the same as the Format function. The Format method is in the CoreLib System.String namespace. If you're confused about why there are two different "Format" statements in VB.NET, you're not alone. Mistaking one for the other can lead to a very subtle bug. Your program might compile and run, but you won't get the correct result!

To check which Format you're using, right click on the Format keyword in the code editor window and select Go To Definition from the popup context menu. The Visual Studio Object Browser will tell you which namespace is being used.

The Format method of the String class can be used to get results that are the same as the function, but you have to learn some new concepts because the method can also do more tricks. In true OOP style, the Format method has five overloads such as:

String.Format(String, Object, Object, Object)

When using the Format method, the String item in the definition above may contain zero or more "format specifiers". The "book definition" of a format specifier provided by Microsoft can be confusing if you're seeing this kind of syntax for the first time.

{index[,alignment][:formatString]}

In more understandable language, just think of a "format specifier" as a "numbered placeholder with parameters". In the example below, the "numbered placeholder" is 0 since there's only one. Note that the definition shows that it's required.

Here's how to code the method to get the same result as the function:

Console.WriteLine( _
   System.String.Format("{0:MMMM d, yyyy}", #12/07/1941#))

One other fact about the Format method of the String class is that it's a "Shared" method. In other words, you don't have to have an "instance" of a string to use it. Consider "ToUpper" - an example of a String method that is an "unshared" method - to see the difference. You have to have an actual string instance to use ToUpper:

Dim myString As String = "abcdef"
Dim myNewString As String
myNewString = myString.ToUpper

Coding something similar to the syntax of the Format method ...

String.ToUpper()

... simply creates a syntax error, "Reference to a non-shared member requires an object reference." To see which methods are shared and which are not, you have to consult the Microsoft documentation.

Formatting Strings with the ToString method

Before discussing some of the other Format options, keep in mind that there's another way to format an object that might be easier for you: the ToString method. And it doesn't even use the Format keyword.

Recall that ToString is one of the fundamental methods in the grandaddy of all objects: the Object object. This means that it's inherited by all objects. Overloaded ToString methods for objects that inherit ToString let you format the string that results from the ToString method.

Let's start with the same example we have already looked at. Here's how to use ToString to format a date:

Dim theDate As Date = #12/25/2005#
TextBox1.Text = theDate.ToString("MMMM d, yyyy")

More good news: adding culture information is easy! Suppose you want to display the date from a structure in, say, Spain. Just add a CultureInfo object.

Dim MyCulture As _
   New System.Globalization.CultureInfo("es-ES")
CultureDateEcho.Text = _
   theDate.ToString("MMMM d, yyyy", MyCulture)

The result is:

diciembre 7, 1941

The culture code is a property of the MyCulture object. The CultureInfo object is an example of a provider. The constant "es-ES" isn't being passed as a parameter; an instance of the CultureInfo object is. Search the VB.NET Help system for CultureInfo to see the list of supported cultures. You can also use a CultureInfo object with the Format method. The code to do this is as follows:

   Dim MyCulture As _
      New System.Globalization.CultureInfo("es-ES")
   CultureFormatDateEcho.Text = _
      System.String.Format(MyCulture, "{0:MMMM d, yyyy}", theDate)

All five ways to format a date are shown in the illustration.

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

Format Specifiers and the Format Method

A lot of the power and flexibility of the Format method is a result of the use of format specifiers. Just to be clear, here's that definition again:

{index[,alignment][:formatString]},

The format specifier we've been using so far is:

"{0:MMMM d, yyyy}"

One of the five overloads of the Format method that we looked at earlier is:

String.Format(String, Object, Object, Object)

The "format specifier" above is the String in the syntax definition above.

All of the permissable format specifiers are defined in .NET but there are so many that you will probably have to look at a reference to remember them. Look up "Formatting Types" at MSDN to see the wide array of choices. On the next page, however, we build a more complex example to show what can be done with format specifiers.

Explore Visual Basic
By Category
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Visual Basic
  4. Using VB.NET
  5. Formatting Strings, Numbers, and Other Objects

©2009 About.com, a part of The New York Times Company.

All rights reserved.