1. Computing
Send to a Friend via Email

The Parse Method

It's the opposite (almost) of ToString!


The Murach book Visual Basic 2010 (reviewed here) defines Parse this way:

"The Parse method is a shared method that performs the reverse operation of the ToString method. In other words, it converts a string value to another data type."

Before reading this for my review, I had never thought of Parse that way. It's a great way to classify it in your mind so you can remember it more clearly. It's a good example of how Murach's books cut to the heart of the matter. To add to what is in Murach, the static Parse method of an object will convert something into the type of the object. Short.Parse converts to Short, DateTime.Parse converts to a DateTime.

But Parse is not quite "the reverse operation of the ToString method".

For one thing, ToString is one of the "big five" methods that are in Object, the root of all of .NET. (The big five are: Equals, GetHashCode, GetType, ReferenceEquals, ToString.) So you can be guaranteed that anything will have an implementation of ToString. But only "types" have an implementation of Parse, for reasons that you will see. The guarantee isn't as strong that an implementation of Parse will exist.

For another, the syntax is completely different because Parse is a static method of the type of the objects themselves while ToString is an overridden method inherited from Object. The Short type was picked more or less at random in the examples below. As noted, most types provide a Parse method.

Dim myObject As Object
Dim myOtherObject As Object
myOtherObject = myObject.ToString()
myOtherObject = Short.Parse(myObject)

The Microsoft documentation always states that the argument to Parse is a string, but that's not a requirement. Although I can't imagine why you would ever want to do it (because you just get the same Short value back again) this works:

myObject = 12345
myOtherObject = Short.Parse(myObject)

ToString will give you something (often, something useless) in any circumstance, Parse will crash if you don't provide an argument that is appropriate for the type. This crashes with a FormatException:

myObject = "Mary had a little lamb."
myOtherObject = Short.Parse(myObject)

For Short.Parse, you only get a result from a string that contains an unformatted (no commas, dollar signs and so forth), signed integer, although you can include white space before and after. The object myOtherObject contains the value -12345 with the type Short after the code below executes.

myObject = " -12345   "
myOtherObject = Short.Parse(myObject)

Because Parse can make your app crash, .NET also provides a TryParse method to help you intercept an exception. Like Parse, TryParse is a method of each type (Short.TryParse(), DateTime.TryParse() and so forth). TryParse will return a 0 if the conversion fails.

One way that Parse is a parallel of ToString is that many objects that implement Parse also provide an overload that lets you format the result. (See my Quick Tip, The ToString Method for more about using this with ToString.) For example, DateTime.Parse lets you include both a format provider and culture-specific formatting information.

DateTime.Parse is also exceptionally flexible in it's ability to turn just about any string that might be a date or a time into one. All of these work:

DateTime.Parse("January 2, 2011")
DateTime.Parse("Jan 02 2011")
DateTime.Parse("Sun, 02 Jan 2011 16:35:00 GMT")

Even so, you should always use TryParse when working with DateTime.Parse to avoid unpleasant runtime exceptions.

Another widely used Parse method can be found in the Enum object. You can get the underlying enumeration from an Enum using the Enum.Parse method. Suppose you have an enumeration like this:

Public Enum myEnum
End Enum

Then you can get the underlying enumeration using Enum.Parse like this:

Dim myInstance As New myEnum
Dim str As String = "lamb"
Dim num As Integer =
    CInt(System.Enum.Parse(myInstance.GetType, str))
    """" & str & """" & " is word number " &
    num.ToString & " in the phrase.")

"lamb" is word number 4 in the phrase.

(I hate it when Microsoft buries little 'unknowns' in their examples so let me also explain that "[as]" is in brackets in the Enum because it will be interpreted as a keyword without the brackets.)

See More About
  1. About.com
  2. Computing
  3. Visual Basic
  4. Quick Tips
  5. Parse - The Opposite of ToString in VB.NET

©2014 About.com. All rights reserved.