September 17th, 2007
Enums, or enumerations, as you probably already know, allows you to create a set of logically related constant values. Using them means you can avoid hard coding numbers and you make your code more readable and maintainable because you’re using memorable and logical names for the values.
One problem that VB6 programmers often ran into was that there was no easy way to extract the matching string for a value. This typically led to clumsy conversion code and/or database tables that had to be maintained whenever the Enum changed. However, in .NET, we now have the System.Enum object to take care of this situation and more. In this article, we will take a look at some of the useful functions in this object.
The first function we’ll look at is GetName. As you might suspect, this returns the string associated with an Enum value. Here’s what the code looks like, first our example Enum declaration, followed by the GetName call.
Public Enum EmailTypes Introduction Sales_Flyer Thank_You Important_Notice Holiday_Greetings Other End Enum .............. EnumValue = System.Enum.GetName(GetType(EmailTypes), EmailTypes.ThankYou).Replace("_", " ")
In this case our return value would be “Thank_You”. I used the underscore in the enum name to make it easy to replace it with a space for display purposes. You can’t have a variable name, or an Enum member name, with a space.
OK, that’s one value. What if you wanted them all so that you could add them to a combo or list box control? That’s what GetNames is for. Here’s the code, using our same example Enum above.
lstEmailType.Items.AddRange([Enum].GetNames(GetType(EmailTypes))) . . .
Of course, this doesn’t remove the underscore from the strings but this would be easy enough to add if you needed it with a simple loop. Also note that we used “[Enum]” in the code above rather than System.Enum. Coding it in the square brackets shows that we’re using the object rather than the statement of the same name.
Sometimes you might want to return all of the values in an Enum at once. For this, you would use the GetValues function as seen here:
Dim EmailValues As Array EmailValues = [Enum].GetValues(GetType(EmailTypes)) For Each EmailValue As Integer In EmailValues 'more code goes here Next
This function makes it easier for us to iterate through the values in an enum.
This function is used to determine if a string name or a numeric value is defined in the Enum. For example…
If [Enum].IsDefined(GetType(EmailTypes), "Thank_You") Then ............ If [Enum].IsDefined(GetType(EmailTypes), 9) Then
…the first If would evaluate to True since we do have an Enum named “Thank_You” in our example. The second would return False because we don’t have that value in our example Enum.
That’s all for this tutorial. There are a few more methods in the Enum object that you might want to explore on your own that I didn’t cover here. Let me know if you have any questions or observations about this article by leaving me a comment.
Rate This Article: