EN
|
NL
jan 9

Written by: André Obelink (Host)
9-1-2009 16:58 

Enumerations can help you to make your code more consistent and better readable. An enumeration is group of constants which you can use to assign as a value of variable. The .NET Framework makes frequently use of enumerations. I think you will be familiar with the following concept: you can select an item from a list of possible values:
 

Make us of the DriveType enumeration


At the end the item of the enumeration is nothing more then an Integer or perhaps better said, a numeric value. It is not hard to create your own enumerations and assign this enumeration as datatype of a variable. Consider the following example. We define an enumeration named CityEnum, which contains two items. In the Button1_Click() event we have to deal with the city number '1'. Because we have set Option Strict On, we must cast the variable cityNumber explicit to the type CityEnum. When Option Strict is not set, you can simply assign the variable directly. As you can see, working with numeric values of enumerations is quite easy.

   1: Option Strict On
   2: 
   3: Public Class Form1
   4: 
   5:     Public Enum CityEnum
   6:         Amsterdam = 0
   7:         Seattle = 1
   8:     End Enum
   9: 
  10:     Private Sub Button1_Click() Handles Button1.Click
  11: 
  12:         Dim cityNumber As Integer = 1
  13: 
  14:         ' Use CType() if 'Option Strict On' is set
  15:         Dim selectedCity As CityEnum = CType(cityNumber, CityEnum)
  16: 
  17:         ' Do 'something' with enum (with help of IntelliSense!)
  18:         Select Case selectedCity
  19:             Case CityEnum.Amsterdam
  20:                 Console.WriteLine("People speak Dutch")
  21:             Case CityEnum.Seattle
  22:                 Console.WriteLine("People speak English")
  23:         End Select
  24: 
  25:     End Sub
  26: 
  27: End Class

 In some cases however, you don't want to deal with the Integer value of the enumeration, but with the 'readable' text value. In our case with the values 'Amsterdam' and 'Seattle'. You can imagine that your XML file is much better readable when use the descriptive values, like Amsterdam in stead of 0. In the next code sample I will show you how you can convert the values from the descriptive text to the real enumeration and vice versa. In this sample I will a fill a ComboBox with all values of our CityEnum. Only one line of code is needed. You can use the [enum].GetNames() method to retrieve all items from the enumeration.
When you select an item you want to get the real enum item based on the text. You can do this also with only one line of code! The magic happens with help of the [enum].Parse() method. In the last statement, when showing the actual message, you can see that calling the ToString() method on the variable city will give you the descriptive value of the enumeration.

   1: Private Sub Form1_Load() Handles MyBase.Load
   2: 
   3:     ' Fill a ComboBox with all items of the specified enumeration
   4:     CityComboBox.Items.AddRange([Enum].GetNames(GetType(CityEnum)))
   5: 
   6: End Sub
   7: 
   8: Private Sub CityComboBox_SelectedIndexChanged() _
   9:                                 Handles CityComboBox.SelectedIndexChanged
  10: 
  11:     Dim message As String = "People in {0} speak {1}"
  12:     Dim language As String = ""
  13: 
  14:     ' Get the name of the selected city
  15:     Dim enumString As String = CityComboBox.Text
  16: 
  17:     ' Convert the 'string' to a real 'enum' value
  18:     Dim city As CityEnum = [Enum].Parse(GetType(CityEnum), enumString)
  19: 
  20:     ' Do 'something' with enum (with help of IntelliSense!)
  21:     Select Case city
  22:         Case CityEnum.Amsterdam
  23:             language = "Dutch"
  24:         Case CityEnum.Seattle
  25:             language = "English"
  26:     End Select
  27: 
  28:     ' Show message --> call enum.ToString() to get the descriptive value
  29:     MessageBox.Show(String.Format(message, city.ToString(), language))
  30: 
  31: End Sub

 

image

Happy coding!

Tags:

Your name:
Your email:
(Optional) Email used only to show Gravatar.
Your website:
Title:
Comment:
Security Code
Enter the code shown above in the box below
Add Comment   Cancel 
Blog
Blog archive
 

Willem Dreeslaan 63, 7103 JE  Winterswijk  |  PO box 428, 7100 AK Winterswijk Netherlands  |  T +31647914464  |  E info@maryor.nl