Every so often, a weird concept crosses my path and I can only sit and wonder as to why a certain method or technology works the way it does, or doesn't work the way I want it to work. A string enumeration is such a case. Now look, I am not the tastiest sweet in the pack, but some things I find hard to fathom at first, but then I go back to some of my old books and realize that I am yet again attempting to break the very rules of a language that makes the language work. Today, I will show you a quick way to make an Enum accept String values instead of an ordinary named constant.
An Enum is a list of named constants that, well, provides a list of options. These options could be a list of settings or whatever you decide it be, except for a list of strings that actually contain spaces. A list of other data types and structures can be found here.
Let's Create a Small Project
Create a new Visual Basic Windows Forms project. Once the form displays, add a big TextBox and one Button. Before we start with the form's code, add a new Class. This class will host the capabilities to create our String Enum. I have named my class StringEnum.
Add the following Namespace:
Directly quoted from MSDN: The System.Reflection namespace contains types that retrieve information about assemblies, modules, members, parameters, and other entities in managed code by examining their metadata.
Add the following to add the StringValue class and give it a property named Value:
Public Class StringValue Inherits System.Attribute Private ReadOnly _value As String Public Sub New(value As String) _value = value End Sub Public ReadOnly Property Value() As String Get Return _value End Get End Property End Class
Straightforward. The class is created. It is given a Constructor and a Property named Value. Now, let's get complicated! Add another class and name it StringEnum.
Public NotInheritable Class StringEnum Private Sub New() End Sub Public Shared Function GetStringValue(value As [Enum]) As String Dim output As String = Nothing Dim type As Type = value.[GetType]() Dim fi As FieldInfo = type.GetField(value.ToString()) Dim attrs As StringValue() = _ TryCast(fi.GetCustomAttributes(GetType(StringValue), _ False), StringValue()) If attrs.Length > 0 Then output = attrs(0).Value End If Return output End Function End Class
The StringEnum class creates a Type object that depends on the value that gets supplied to this class. Then, it creates a FieldInfo object that converts the supplied value Type to a string with the help of the StringValue class.
Add the following code to create an Enum capable of hosting string values:
Public Enum GameColorModes <StringValue("Black")> _ Black = 1 <StringValue("Green")> _ Green = 2 <StringValue("Blue")> _ Blue = 3 <StringValue("Red")> _ Red = 4 <StringValue("White")> _ White = 5 End Enum
There you go! Voilà. Now, let's put this Enum to good use. Add the following Function to your Form:
Private Function GetColorCodes(intColor As Integer) As String Dim strColorDesc As String = "" Select Case DirectCast(intColor, GameColorModes) Case GameColorModes.White strColorDesc = StringEnum.GetStringValue(GameColorModes.White) Exit Select Case GameColorModes.Blue strColorDesc = StringEnum.GetStringValue(GameColorModes.Blue) Exit Select Case GameColorModes.Green strColorDesc = StringEnum.GetStringValue(GameColorModes.Green) Exit Select Case GameColorModes.Red strColorDesc = StringEnum.GetStringValue(GameColorModes.Red) Exit Select Case GameColorModes.Black strColorDesc = StringEnum.GetStringValue(GameColorModes.Black) Exit Select Case Else strColorDesc = "Undefined" Exit Select End Select Return strColorDesc End Function
This function determines what value has been sent through, and then converts the received value to the Enum's value. Lastly, it stores the Enum's value into a string variable named strColorDesc. Add the last piece of the puzzle:
Private Sub Button1_Click(sender As Object, e As EventArgs) _ Handles Button1.Click TextBox1.Text += GetColorCodes(1) & Environment.NewLine TextBox1.Text += GetColorCodes(2) & Environment.NewLine TextBox1.Text += GetColorCodes(3) & Environment.NewLine TextBox1.Text += GetColorCodes(4) & Environment.NewLine TextBox1.Text += GetColorCodes(5) & Environment.NewLine TextBox1.Text += GetColorCodes(6) & Environment.NewLine End Sub
Once run, the application will resemble Figure 1.
Figure 1: The String Enum in action
Giving enumerations the capability to host String values was a bit complicated, yes, but it is worth it. You will make use of this feature many-a-time. Until next time, cheers!