Define String ENUM in VB.Net Define String ENUM in VB.Net windows windows

Define String ENUM in VB.Net


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_ContanctEnd EnumDim 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)


You could just create a new type

''' <completionlist cref="Test"/>Class Test    Private Key As String    Public Shared ReadOnly Contact  As Test = New Test("Personal Contanct")    Public Shared ReadOnly PersonalInfo As Test = New Test("Personal Info")    Private Sub New(key as String)        Me.Key = key    End Sub    Public Overrides Function ToString() As String        Return Me.Key    End FunctionEnd Class

and when you use it, it kinda looks like an enum:

Sub Main    DoSomething(Test.Contact)    DoSomething(Test.PersonalInfo)End SubSub DoSomething(test As Test)    Console.WriteLine(test.ToString())End Sub

output:

Personal Contanct
Personal Info


How about using Tagging. Something like:

Public Enum MyEnum<StringValue("Personal Contact")>Contact<StringValue("My PersonalInfo")>PersonalInfoEnd Enum

You would have to write the StringValue attribute as:

Public Class StringValueAttribute    Inherits Attribute    Public Property Value As String    Public Sub New(ByVal val As String)        Value = val    End SubEnd Class

To get it out:

 Public Function GetEnumByStringValueAttribute(value As String, enumType As Type) As Object    For Each val As [Enum] In [Enum].GetValues(enumType)        Dim fi As FieldInfo = enumType.GetField(val.ToString())        Dim attributes As StringValueAttribute() = DirectCast(fi.GetCustomAttributes(GetType(StringValueAttribute), False), StringValueAttribute())        Dim attr As StringValueAttribute = attributes(0)        If attr.Value = value Then            Return val        End If    Next    Throw New ArgumentException("The value '" & value & "' is not supported.")End FunctionPublic Function GetEnumByStringValueAttribute(Of YourEnumType)(value As String) As YourEnumType    Return CType(GetEnumByStringValueAttribute(value, GetType(YourEnumType)), YourEnumType)End Function

And then a call to get the Enum (using string attribute):

Dim mEnum as MyEnum = GetEnumByStringValueAttribute(Of MyEnum)("Personal Contact")

To get the "Attribute" value out (removed handling 'Nothing' for clarity):

  Public Function GetEnumValue(Of YourEnumType)(p As YourEnumType) As String        Return DirectCast(Attribute.GetCustomAttribute(ForValue(p), GetType(StringValueAttribute)), StringValueAttribute).Value  End Function  Private Function ForValue(Of YourEnumType)(p As YourEnumType) As MemberInfo        Return GetType(YourEnumType).GetField([Enum].GetName(GetType(YourEnumType), p))  End Function

And the call to get the string attribute (using Enum):

Dim strValue as String = GetEnumValue(Of MyEnum)(MyEnum.Contact)