O'Reilly logo

Microsoft® Visual Basic® .NET Programmer's Cookbook by Matthew MacDonald

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Convert the Name of an Enumerated Value into the Value

Problem

You want to set an enumeration using the string name of a value, not the integer value, or you want to retrieve all the names used for constants in an enumeration.

Solution

Use the Enum.GetNames method to get an array of all enumeration names and the Enum.Parse method to convert a string into the corresponding value from an enumeration.

Discussion

An enumeration is a group of integer constants with descriptive names. Usually, you’ll use enumeration values by name. Sometimes, however, it’s necessary to convert enumeration values into strings, and vice versa. One reason might be to provide a user with a list of enumerated values and give them the chance to choose one.

As an example, consider ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required