Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

EnumConverter Class

Provides a type converter to convert Enum objects to and from various other representations.

For a list of all members of this type, see EnumConverter Members.

System.Object
   System.ComponentModel.TypeConverter
      System.ComponentModel.EnumConverter

[Visual Basic]
Public Class EnumConverter
   Inherits TypeConverter
[C#]
public class EnumConverter : TypeConverter
[C++]
public __gc class EnumConverter : public TypeConverter
[JScript]
public class EnumConverter extends TypeConverter

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Remarks

This converter can only convert an enumeration object to and from a string. The EnumConverter provides the Comparer property to get an IComparer interface that can be used to sort the values of the enumeration. By default, the enumeration values are sorted in the order they appear in the file.

For more information about type converters, see the TypeConverter base class and Implementing a Type Converter.

Note   You should never create an instance of an EnumConverter. Instead, call the GetConverter method of TypeDescriptor. For more information, see the examples in the TypeConverter base class.

Notes to Inheritors:  If you want to sort the values into a different order, override the Comparer property.

Example

[Visual Basic, C#, C++] The following sample converts a variable of type Enum to a string, and vice versa. The code segment assumes that you have declared an Enum called Servers and that it has the following members, Windows=1, Exchange=2, BizTalk=3

[Visual Basic] 
Dim myServer As Servers = Servers.Exchange
Dim myServerString As string = "BizTalk"
Console.WriteLine(TypeDescriptor.GetConverter(myServer).ConvertTo(myServer, GetType(String))) 
Console.WriteLine(TypeDescriptor.GetConverter(myServer).ConvertFrom(myServerString))    
            

[C#] 
Enum myServer= Servers.Exchange;
string myServerString = "BizTalk";
Console.WriteLine(TypeDescriptor.GetConverter(myServer).ConvertTo(myServer, typeof(string))); 
Console.WriteLine(TypeDescriptor.GetConverter(myServer).ConvertFrom(myServerString));    


[C++] 
// Requires public declaration of the following type.
// __value enum Servers {Windows=1, Exchange=2, BizTalk=3};
Servers myServer = Servers::Exchange;
String* myServerString = S"BizTalk";
Console::WriteLine(TypeDescriptor::GetConverter(__box(myServer))->ConvertTo(__box(myServer), __typeof(String)));
Console::WriteLine(TypeDescriptor::GetConverter(__box(myServer))->ConvertFrom(myServerString));

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.ComponentModel

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

Assembly: System (in System.dll)

See Also

EnumConverter Members | System.ComponentModel Namespace

Show:
© 2014 Microsoft