1. Technology

Enum - A Building Block of Visual Basic

Enums help make programming easier

By

Dirk Hünniger/Wikimedia Commons
Updated July 01, 2014

The Enum idea is simple!

Enums (enumerations) are structures you define when you need a set of constant values. In VB 6, they were considered important enough for Microsoft to provide a custom dialog (the Enum Builder, part of the Class Builder utility). Just like the rest of VB, .NET took a good idea and ran with it.

To get the idea of an Enum across, an example is better than anything else. (An Enum must be declared "outside of any method body".)

Enum MakeOfCar
   Ford
   Toyota
   Volvo
   Peugeot
   Fiat
End Enum
Private Sub ... (remainder of Sub statement omitted)
   MsgBox(MakeOfCar.Volvo)
End Sub

Pretty simple! An Enum lets you use the more easily remembered name "Volvo" in your code. In fact, Visual Studio's Intellisense will prompt you with your declared names to make it even easier. But if you run the code above, you will discover that it doesn't display the string "Volvo", it displays the integer 2! A major limitation of Enum's is that it must be declared as some sort of integer. (Byte, Short, ULong ... There are eight in VB.NET 2005. Int32 - same as Integer - is the default.) So, Enums often need a little help.

The Microsoft documentation tells you that Enums are a data type. That means that you can declare your own variables using a new Enum as the data type:

Dim myCar As MakeOfCar
myCar = MakeOfCar.Volvo
msgbox(myCar) ' Displays 2

If you want to display the string "Volvo" instead, you can use the always available ToString method if you're starting with an element of the Enum like MakeOfCar.Volvo. (ToString is a fundamental method of the granddaddy of all classes, the Object class.)

Dim myCar As MakeOfCar
myCar = MakeOfCar.Volvo
MsgBox(myCar.ToString)

Suppose, however, that you want to go the other way. That is, suppose your program receives the string "Peugeot" as input and you want to get the integer value of the Enum. .NET to the rescue! As it turns out, there is a method just for that: Parse(). First, here's the code:

myCar = System.Enum.Parse(GetType(MakeOfCar), "Peugeot")
MsgBox(myCar) ' Displays 3

The way this code works isn't exactly transparent. One of the problems is that it's not obvious to many students of VB what that GetType() method does. Here's the explanation.

One of the architectural advances of .NET is to make use of interface objects that are mainly just used to make other objects work together. (If you have read my GDI+ series of tutorials, you saw the Graphics object used that way.) Type is an object that is used like that. It's worth taking a few moments to let Visual Studio show you just what is included in the Type object when you run the code.

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

The illustration answers the question. A lot of stuff! They pack so much into it so a Type object can be used to allow another method, like the Enum Parse() method, to do its magic by simply picking what it needs out of the Type object. Parse then looks through the MakeOfCar type, finds the string "Peugeot", and returns the integer index.

In the first example above, I used the ToString method to get the string that represents an integer element of an Enum when the program started with an element of the Enum. If you just have the value of the Enum, like the integer 3, you can get the string using the GetName() method in the Enum object with the type object for the MakeOfCar Enum.

MsgBox(System.Enum.GetName(GetType(MakeOfCar), 3))

You can also get arrays of the names and their integer equivalents with the Enum methods, GetNames() and GetValues().

The IsDefined() method of the Enum object is very useful in avoiding runtime exceptions when your code is executed. Here's an example: If you try to execute this code ...

Dim CarModel As String
CarModel = "Hupmobile"
MsgBox(System.Enum.Parse(GetType(MakeOfCar), CarModel))

... an Argument Exception is thrown at runtime. To solve the problem, do this:

Dim CarModel As String
CarModel = "Hupmobile"
If System.Enum.IsDefined(GetType(MakeOfCar), CarModel) Then
   MsgBox(System.Enum.Parse(GetType(MakeOfCar), CarModel))
Else
   MsgBox("Sorry, Not Defined")
End If

article illustrations © Dan Mabbutt

  1. About.com
  2. Technology
  3. Visual Basic
  4. Using VB.NET
  5. Enum - A Building Block of Visual Basic

©2014 About.com. All rights reserved.