将枚举名称检索为字符串

时间:2016-11-16 13:45:00

标签: vb.net enums

如何将字符串的名称检索为字符串?我知道你可以得到积分值,但这不是我想要的。

我搜索了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 个答案:

答案 0 :(得分:3)

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

如果你喜欢更长的方式,这是另一种方式:

[Enum].GetName(PipeEndTreatmentEnum.SetOn.GetType(), PipeEndTreatmentEnum.SetOn)
相关问题