Share via


Enum.GetName メソッド

指定した値を持つ指定した列挙体にある定数の名前を取得します。

Public Shared Function GetName( _
   ByVal enumType As Type, _   ByVal value As Object _) As String
[C#]
public static string GetName(TypeenumType,objectvalue);
[C++]
public: static String* GetName(Type* enumType,Object* value);
[JScript]
public static function GetName(
   enumType : Type,value : Object) : String;

パラメータ

  • enumType
    列挙型。
  • value
    基になる型に関連して列挙される特定の定数の値。

戻り値

値が value である enumType 内にある列挙型定数の名前が含まれた文字列。そのような定数が見つからない場合は、 null 参照 (Visual Basic では Nothing) 。

例外

例外の種類 条件
ArgumentNullException enumType または value が null 参照 (Visual Basic では Nothing) です。
ArgumentException enumTypeEnum ではありません。

または

value の型が enumType ではなく、基になる型も enumType と同じではありません。

使用例

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

[C#] 
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));
    }
}

[C++] 
#using <mscorlib.dll>
using namespace System;

__value enum Colors { Red, Green, Blue, Yellow };
__value enum Styles { Plaid, Striped, Tartan, Corduroy };

int main() {
    Console::WriteLine("The 4th value of the Colors Enum is {0}", Enum::GetName(__typeof(Colors), __box(3)));
    Console::WriteLine("The 4th value of the Styles Enum is {0}", Enum::GetName(__typeof(Styles), __box(3)));
}

[JScript] 
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 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, Common Language Infrastructure (CLI) Standard

参照

Enum クラス | Enum メンバ | System 名前空間