Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>How about using Tagging. Something like:</p> <pre><code>Public Enum MyEnum &lt;StringValue("Personal Contact")&gt;Contact &lt;StringValue("My PersonalInfo")&gt;PersonalInfo End Enum </code></pre> <p>You would have to write the StringValue attribute as:</p> <pre><code>Public Class StringValueAttribute Inherits Attribute Public Property Value As String Public Sub New(ByVal val As String) Value = val End Sub End Class </code></pre> <p>To get it out:</p> <pre><code> 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 '" &amp; value &amp; "' is not supported.") End Function Public Function GetEnumByStringValueAttribute(Of YourEnumType)(value As String) As YourEnumType Return CType(GetEnumByStringValueAttribute(value, GetType(YourEnumType)), YourEnumType) End Function </code></pre> <p><strong>And then a call to get the Enum (using string attribute):</strong></p> <pre><code>Dim mEnum as MyEnum = GetEnumByStringValueAttribute(Of MyEnum)("Personal Contact") </code></pre> <p>To get the "Attribute" value out (removed handling 'Nothing' for clarity):</p> <pre><code> 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 </code></pre> <p><strong>And the call to get the string attribute (using Enum):</strong></p> <pre><code>Dim strValue as String = GetEnumValue(Of MyEnum)(MyEnum.Contact) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload