But 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 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 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 overloaded methods 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 it 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:
Dim theDate As Date
theDate = #12/25/2006#
DateEcho.Text = System.String.Format("{0:MMMM d, yyyy}", theDate)
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 25, 2005
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.

