Retrieves the name of the constant in the specified enumeration that has the specified value.
Namespace:
System
Assembly:
mscorlib (in mscorlib.dll)
Visual Basic (Declaration)
<ComVisibleAttribute(True)> _
Public Shared Function GetName ( _
enumType As Type, _
value As Object _
) As String
Dim enumType As Type
Dim value As Object
Dim returnValue As String
returnValue = Enum.GetName(enumType, _
value)
[ComVisibleAttribute(true)]
public static string GetName(
Type enumType,
Object value
)
[ComVisibleAttribute(true)]
public:
static String^ GetName(
Type^ enumType,
Object^ value
)
public static function GetName(
enumType : Type,
value : Object
) : String
Parameters
- enumType
- Type: System..::.Type
An enumeration type.
- value
- Type: System..::.Object
The value of a particular enumerated constant in terms of its underlying type.
Return Value
Type:
System..::.StringA string containing the name of the enumerated constant in enumType whose value is value, or nullNothingnullptra null reference (Nothing in Visual Basic) if no such constant is found.
| Exception | Condition |
|---|
| ArgumentNullException |
enumType or value is nullNothingnullptra null reference (Nothing in Visual Basic). |
| ArgumentException |
enumType is not an Enum. -or-
value is neither of type enumType nor does it have the same underlying type as enumType. |
The following example illustrates the use of GetName.
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()
Console.WriteLine("The 4th value of the Colors Enum is {0}", [Enum].GetName(GetType(Colors), 3))
Console.WriteLine("The 4th value of the Styles Enum is {0}", [Enum].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() {
Console.WriteLine("The 4th value of the Colors Enum is {0}", Enum.GetName(typeof(Colors), 3));
Console.WriteLine("The 4th value of the Styles Enum is {0}", Enum.GetName(typeof(Styles), 3));
}
}
using namespace System;
enum class Colors
{
Red, Green, Blue, Yellow
};
enum class Styles
{
Plaid, Striped, Tartan, Corduroy
};
int main()
{
Console::WriteLine( "The 4th value of the Colors Enum is {0}", Enum::GetName( Colors::typeid, 3 ) );
Console::WriteLine( "The 4th value of the Styles Enum is {0}", Enum::GetName( Styles::typeid, 3 ) );
}
import System;
public class GetNameTest {
enum Colors { Red, Green, Blue, Yellow };
enum Styles { Plaid, Striped, Tartan, Corduroy };
public static function Main() {
Console.WriteLine("The 4th value of the Colors Enum is {0}", Enum.GetName(Colors, 3));
Console.WriteLine("The 4th value of the Styles Enum is {0}", Enum.GetName(Styles, 3));
}
}
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
.NET Framework
Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
Reference