「VB.NET」Enumの要素名と値を全て取得する

2022年1月17日

書式
1.Enumクラスの宣言
Enum 列挙型名
要素
End Enum
2.Enumの要素を取得
For Each 変数名 As Enum名 In [Enum].GetValues(GetType(Enum名))
処理コード
Next

使用例

Module Module1
    'EnumクラスMonth
    Enum Month
        January = 1
        February
        March
        April
        May
        June
        July
        August
        September
        October
        November
        December
    End Enum

    Public Sub Main()

        'Enumの要素名、値が出力
        For Each mthLst As Month In [Enum].GetValues(GetType(Month))
            '値
            Dim intVal As Integer = mthLst
            '要素名
            Dim strName As String = mthLst.ToString()
            '「要素名 =値」を出力
            Console.WriteLine(strName & " = " & intVal)
        Next

        Console.ReadKey()
    End Sub
End Module
実行結果
January = 1
February = 2
March = 3
April = 4
May = 5
June = 6
July = 7
August = 8
September = 9
October = 10
November = 11
December = 12

実行結果

January = 1
February = 2
March = 3
April = 4
May = 5
June = 6
July = 7
August = 8
September = 9
October = 10
November = 11
December = 12

 

VB.net

Posted by arkgame