Deborah's Developer MindScape






         Tips and Techniques for Web and .NET developers.

August 10, 2010

Enum.TryParse

Filed under: C#,VB.NET @ 1:36 am

If you work with Enum values, you may often need to convert between the Enum, an integer representation of the Enum (to store in a table for example), and the string representation of the Enum (to display to the user for example). .NET 4.0 makes this easier with the new TryParse method.

This example uses a CustomerTypeOption Enum value.

In C#:

public enum CustomerTypeOption
{
    /// <summary>
    /// Customer type is unknown
    /// </summary>
    Unknown = 0,

    /// <summary>
    /// Customer is an individual
    /// </summary>
    Individual = 1,

    /// <summary>
    /// Customer is a corporation
    /// </summary>
    Corporation = 2,

    /// <summary>
    /// Customer is an educational institution
    /// </summary>
    Education = 3,

    /// <summary>
    /// Customer is a government agency.
    /// </summary>
    Government = 4
}

In VB:

Public Enum CustomerTypeOption
    ”’ <summary>
    ”’ Customer type is unknown
    ”’ </summary>
    Unknown = 0

    ”’ <summary>
    ”’ Customer is an individual
    ”’ </summary>
    Individual = 1

    ”’ <summary>
    ”’ Customer is a corporation
    ”’ </summary>
    Corporation = 2

    ”’ <summary>
    ”’ Customer is an educational institution
    ”’ </summary>
    Education = 3

    ”’ <summary>
    ”’ Customer is a government agency.
    ”’ </summary>
    Government = 4
End Enum

The Enum defines the valid types of customers supported in an application.

The TryParse method takes either the integer value (converted to a string) OR the string value and converts it to the Enum type as shown below.

In C#:

int i = 1;
string value = i.ToString();
CustomerTypeOption customerType;
if (Enum.TryParse(value, out customerType))
{ /* Success */ }
else
{ /* Failure */ }

value = "individual";
if (Enum.TryParse(value, true, out customerType))
{ /* Success */ }
else
{ /* Failure */ }

In VB:

Dim i As Integer = 1
Dim value As String = i.ToString()
Dim customerType As CustomerTypeOption
If ([Enum].TryParse(value, customerType)) Then
    ‘ Success */
Else
    ‘ Failure
End If

value = "individual"
If ([Enum].TryParse(value, True, customerType)) Then
    ‘ Success
Else
    ‘ Failure
End If

In the first example, the integer 1 is converted to a string and passed as the first parameter to the TryParse method. A variable of the desired Enum type is passed in as the second parameter and is defined as an output parameter (using the out keyword in C#).

If the TryParse succeeds, the method returns true and the converted value is returned in the output parameter. If the method fails, it returns false.

In the second example, the string "individual" is passed as the first parameter. The second parameter is set to true, telling the TryParse method to ignore the case of the first parameter. This allows "individual" to match "Individual" even if the casing is different. The third parameter is then the output parameter.

Use TryParse any time you need to convert from an integer value or string representation to an Enum value.

Enjoy!

RSS feed for comments on this post. TrackBack URI

Leave a comment

© 2020 Deborah's Developer MindScape   Provided by WPMU DEV -The WordPress Experts   Hosted by Microsoft MVPs