Visual Basic

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

Formatting Strings, Numbers, and Other Objects

Format includes a lot of options!

By Dan Mabbutt, About.com

Nov 3 2007

In an earlier article, I used the VB.NET 2005 Upgrade Wizard to convert this VB6 statement to VB.NET:

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

The result was this VB.NET statement:

DateEcho.Text = CStr(theDate)

Much to my surprise, the VB.NET Upgrade Wizard didn't even attempt to use Format! And it's not clear why because identically the same code could have been used. The following code works just fine in VB.NET:

Dim theDate As Date
theDate = #12/25/2007#
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 a lot of your code manually. The Upgrade Wizard might not have used Format because there are so many options that they might have decided it was just too much complexity to handle in an automated conversion.

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 Visual Basic "Help" system can give you a complete list. Search for "format function" for VB6

The description of the Format function in VB.NET is quite different. Instead of just telling you how to use it, they tell you what it is:

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

The difference in Microsoft's formal description between VB6 and VB.NET is mostly appearance. Style is nearly identical with strFormat.

The VB.NET Format function is just code you can call in a VB.NET library, the Strings module. In other words, the Strings module is just a bag of code supplied by Microsoft, not a formal object. For example, other slightly unrelated functions in this same module are Split and Len. The Format function in VB.NET is part of the Microsoft.VisualBasic namespace and is intended to provide some compatibility with VB6.

Page 2 tells you how to get more with VB.NET!

Explore Visual Basic

By Category

About.com Special Features

Build Your Own Website

Step-by-step advice on how to do everything from choosing a Web host to promoting your content. More >

Connect Your Home Computers

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

Visual Basic

  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.