Define String ENUM in VB.Net

后端 未结 6 1604
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-09 15:37

I am using Window Application for my project. There is situation where i need to define string enum and using it in my project.

i.e.

Dim PersonalInfo         


        
6条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-09 16:19

    For non-integer values, Const in a Structure (or Class) can be used instead:

    Structure Test
        Const PersonalInfo = "Personal Info"
        Const Contanct = "Personal Contanct"
    End Structure
    

    or in a Module for direct access without the Test. part:

    Module Test
        Public Const PersonalInfo = "Personal Info"
        Public Const Contanct = "Personal Contanct"
    End Module
    

    In some cases, the variable name can be used as a value:

    Enum Test
        Personal_Info
        Personal_Contanct
    End Enum
    
    Dim PersonalInfo As String = Test.Personal_Info.ToString.Replace("_"c, " "c)
    
    ' or in Visual Studio 2015 and newer:
    Dim Contanct As String = NameOf(Test.Personal_Contanct).Replace("_"c, " "c)
    

提交回复
热议问题