2016-11-16 40 views
0

如何以字符串形式检索枚举的名称?我知道你可以得到整数值,但这不是我想要的。以字符串形式检索枚举名称

我搜索了www,但它没有显示任何好的样本。

我做了一个示例类,以正确显示我需要的东西。

Class test 

    Public Property PipeEndTreatment As PipeEndTreatmentEnum 
    Public Enum PipeEndTreatmentEnum 
     SetOn 
     SetIn 
     Offset 
     OffsetFlush 
    End Enum 

    Private Sub TestEnumNameValue() 


     PipeEndTreatment = PipeEndTreatmentEnum.SetOn 

     Dim StringValue As String 
     StringValue = "SetOn" ' This value needs to be generated from the PipeEndTreatment property 


    End Sub 

End Class 
+1

'的StringValue = PipeEndTreatment.ToString()' – Blackwood

+0

[转换枚举为String](可能的复制http://stackoverflow.com/questions/ 483794 /转换,枚举到字符串) –

回答

2

只要使用ToString(),例如, PipeEndTreatmentEnum.SetOn.ToString()

这里的的情况下,另一种方式,你喜欢长方式:

[Enum].GetName(PipeEndTreatmentEnum.SetOn.GetType(), PipeEndTreatmentEnum.SetOn)