2016-11-16 62 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)