How to list Enum's members

后端 未结 4 1849
不思量自难忘°
不思量自难忘° 2021-02-04 02:36

How to list Enum\'s members in code? I have the following Enum:

Public Enum TestEnum As int32
    First = 0
    Second = 2
    Third = 4
    Fourth = 6
End Enum
         


        
4条回答
  •  醉梦人生
    2021-02-04 02:44

    You can simply iterate through all values like this:

    Public Enum TestEnum As int32
        First = 0
        Second = 2
        Third = 4
        Fourth = 6
    End Enum
    
    For Each tstEnum As TestEnum In System.Enum.GetValues(GetType(TestEnum))
    
        Response.Write(
            String.Format("Name: {0}  Value: {1}", 
                tstEnum.ToString, 
                CInt(tstEnum).ToString
            )
        )
    
    Next
    

提交回复
热议问题