1. Computing

Formatting Strings, Numbers, and Other Objects

Format is a whole grab bag of options!

By

Updated June 04, 2011

If you use the VB.NET Upgrade Wizard to convert this VB6 statement to VB.NET:


DateEcho.Caption = _
     Format$(theDate, "MMMM d, yyyy")

The result is this VB.NET statement:


DateEcho.Text = CStr(theDate)

The VB.NET Upgrade Wizard doesn't even attempt to use Format! And it's not clear why because code that is substantially the same could have been used. The following code works just fine in VB.NET.


DateEcho.Text = _
     Format$(theDate, "MMMM d, yyyy")

This is just more evidence for my long-held position that when you upgrade a VB6 program to VB.NET, plan on rewriting most of your code. In this case, the Upgrade Wizard might not have used Format because they might have decided it was just too much complexity to handle in an automated conversion. VB6, being long dead now, wouldn't be important except that it's an excellect example of how not even Microsoft's programmers can handle the all the twists and turns of Format.

Explaining that complexity is the goal of this article!

The description of the Format function for VB6 is ...


Format(Expression, strFormat)

... where strFormat is a combination of format characters like "MMMM" for "long month" and "d" for "day" or perhaps a keyword expression like "Medium Time".

The description of the Format function in VB.NET is quite different.


Public Shared Function Format( _
     ByVal Expression As Object, _
     Optional ByVal Style As String = "" _
) As String

The practical difference for day-to-day use between Format in VB6 and Format in VB.NET is mostly appearance. For example, Style is nearly identical with with the VB6 strFormat.

The VB.NET Format function is just shared code you can call. Format in VB.NET is part of the Microsoft.VisualBasic namespace and is intended to provide some compatibility with VB6.

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. There are substantial differences.

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" - "index" in the definition above - is 0 since there's only one. Also note that the definition above states that it's required.

The function (in the Strings object and the method below both display the same thing:


Console.WriteLine(
     Strings.Format(
         #12/7/1941#, "MMMM d, yyyy"))

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

What a difference the letter "s" makes!

One other fact about the Format method of the String class (the second one) 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.

Microsoft provides a rich selection of format specifiers to handle everything from hexidecimal numbers to currency, but since it's impossible for for the inventive minds of the Microsoft programmers to think of absolutely everything, you can also roll your own with custom formatting on the next page.

  1. About.com
  2. Computing
  3. Visual Basic
  4. Using VB.NET
  5. Formatting Strings, Numbers, and Other Objects

©2014 About.com. All rights reserved.