.NET Enums 操作

Tips about .NET Enums
By Alberto Venditti | 20 Apr 2004

Some very simple tips using Enum types in VB.NET and C#.NET
Introduction

The following lines of code show you how to retrieve an Enum member, given its integer value or its name. This can be very useful when you are using Enum types in VB.NET or C#.NET and you need to set an Enum type value loading, for example, a string configuration setting from a .config file.

In the following, we suppose to have these .NET namespaces referenced and this custom Enum type defined:
Collapse

  ' VB.NET version

  Imports System.ComponentModel
  Imports System.Diagnostics

  Public Enum MyEnum
    alfa = 1
    beta = 2
    gamma = 3
  End Enum

Collapse

  // C#.NET version

  using System.ComponentModel;
  using System.Diagnostics;

  public enum MyEnum
  {
    alfa = 1, 
    beta = 2,
    gamma = 3
  }

How to retrieve an Enum member given its integer value

To retrieve an Enum member given its integer value, simply cast the integer value to your Enum type:
Collapse

  ' VB.NET version

  Dim MyEnumVal As MyEnum
  Dim i As Integer

  i = 2
  MyEnumVal = CType(i, MyEnum)
  Debug.WriteLine(MyEnumVal.ToString())

The result of the Debug.WriteLine() method will be "beta".

Notice that if you try to cast an integer value that is not defined in the Enum, the code will work without exceptions; in the following code, MyEnumVal receives the value of 4, anyway:
Collapse

  ' VB.NET version

  i = 4
  MyEnumVal = CType(i, MyEnum)
  Debug.WriteLine(MyEnumVal.ToString())    ' The output is "4"

As suggested by Michael Kennedy (thank you!), with some computing overhead, you can test for defined Enum values using this code:
Collapse

  ' VB.NET version

  If Not MyEnum.IsDefined(GetType(MyEnum), 4) Then
    Debug.WriteLine("The value of 4 is not defined in the Enum")
  End If

See below for the C#.NET version:
Collapse

  // C#.NET version

  MyEnum MyEnumVal;
  int i;

  // Retrieve an enum member by its value

  i = 2;
  MyEnumVal = (MyEnum)i;
  Debug.WriteLine(MyEnumVal.ToString());

  // If the specified value is not an enum member,

  // MyEnumVal receives the value anyway

  i = 4;
  MyEnumVal = (MyEnum)i;
  Debug.WriteLine(MyEnumVal.ToString());    // The output is "4"


  // Test for allowed enum values

  if (!MyEnum.IsDefined(typeof(MyEnum), 4))
    Debug.WriteLine("The value of 4 is not defined in the Enum");

How to retrieve an Enum member given its name

To retrieve an Enum member given its name, use the ConvertFrom() method of the TypeConverter class and cast the result to your Enum type:
Collapse

 ' VB.NET version

 MyEnumVal = CType(TypeDescriptor.GetConverter(MyEnumVal).ConvertFrom("gamma"), MyEnum)
 Debug.WriteLine(MyEnumVal.ToString())    ' The output is "gamma"

An alternative way to reach the same goal is using the Parse() method of System.Enum. This approach, suggested by Dactyl (thank you!), is simpler and 3-times faster than using the TypeConverter class:
Collapse

  ' VB.NET version

  MyEnumVal = CType(System.Enum.Parse(GetType(MyEnum), "gamma"), MyEnum)

See below for the C#.NET version:
Collapse

  // C#.NET version

  MyEnumVal = (MyEnum) TypeDescriptor.GetConverter(MyEnumVal).ConvertFrom("gamma");
  Debug.WriteLine(MyEnumVal.ToString());    // The output is "gamma"


  // Dactyl's alternative

  MyEnumVal = (MyEnum) Enum.Parse(typeof(MyEnum), "gamma");


 

posted @ 2010-10-22 13:48  lavandachen  阅读(275)  评论(0编辑  收藏  举报