Retrieves the name of the constant in the specified enumeration that has the specified value.
A string containing the name of the enumerated constant in enumType whose value is value, or null if no such constant is found.
The following code sample illustrates the use of GetName:
CopyVB.NET
CopyC#
Imports System Public Class GetNameTest Enum Colors Red Green Blue Yellow End Enum 'Colors Enum Styles Plaid Striped Tartan Corduroy End Enum 'Styles Public Shared Sub Main() MessageBox.Show("The 4th value of the Colors Enum is " + EnumHelper.GetName(GetType(Colors), 3)) MessageBox.Show("The 4th value of the Styles Enum is " + EnumHelper.GetName(GetType(Styles), 3)) End Sub 'Main End Class 'GetNameTest
using System; public class GetNameTest { enum Colors { Red, Green, Blue, Yellow }; enum Styles { Plaid, Striped, Tartan, Corduroy }; public static void Main() { MessageBox.Show("The 4th value of the Colors Enum is " + EnumHelper.GetName(typeof(Colors), 3)); MessageBox.Show("The 4th value of the Styles Enum is " + EnumHelper.GetName(typeof(Styles), 3)); } }
| Exception | Condition |
|---|---|
| ArgumentException | enumType is not an System.Enum. -or- value is neither of type enumType nor does it have the same underlying type as enumType. |