The VB.NET Salary Gap3 Quick String Tips

Introduction to System.Enum

September 17th, 2007

Enums allow you to construct better appsEnums, 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
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.


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

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.

Share This Article: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Reddit
  • StumbleUpon
  • Technorati
  • DotNetKicks
  • DZone

Entry Filed under: VB.NET Tutorials, VB6 To VB.NET

Rate This Article:

Not That GoodCould Be BetterOKGoodGreat (1 votes, average: 5 out of 5)
Loading ... Loading ...

1 Comment Add your own

  • 1. 10 Hot ASP.NET Tips - 9/1&hellip  |  September 17th, 2007 at 10:18 pm

    [...] Introduction to System.Enum - This article from the Visual Basic Notebook for .NET introduces us to enumerations and enumerated data types in VB.NET; I prefer C# but I can go back and forth. VBN is an excellent ASP.NET blog and I HIGHLY recommend adding it to your RSS feed. [...]

Leave a Comment


Required, hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed

Visit Me At My New Site, Programming In C#

Most Popular Articles

Highest Rated Articles


Most Recent Articles


 Subscribe in a reader

To subscribe by e-mail
Enter your address here

Delivered by FeedBurner

VB Opportunities