It's been a while that I've been using Enums (enumerations) in .Net. But the problem is I'm using it just like in a C++ style. I've never thought of anything new regarding .Net's approach to enumerations. Until now.
An Enumeration is a collection of constant numeric values. In other words, its simply a group of constants. A general declaration of enums would look like this:
enum sample {
item1 = 0,
item2 = 305,
item3 = 451
}
Enums are very useful specially if your constants are grouped in a special way. Instead of declaring a constant per "group value" you can instead create an enum to give it a more "groupy" view. Well, I think you got my point.
.Net enums are quite special. Since everything in the .Net framework is an object its safe to say that enums are also objects. The .Net framework has an Enum class which contains some static functions that is quite helpful when you're using enums. Here's the list of some useful functions:
| Parse | Converts the string representation of the name or numeric value of one or more enumerated constants to an equivalent enumerated object. |
| GetValues | Retrieves an array of the values of the constants in a specified enumeration. |
| GetUnderlyingType | Returns the underlying type of the specified enumeration. |
| GetName | Retrieves the name of the constant in the specified enumeration that has the specified value. |
| GetNames | Retrieves an array of the names of the constants in a specified enumeration. |
The most useful so far, for me, is the parse method. Let me demonstrate its usefullness. When I'm creating a database system and encounters a field that has a fixed number of values (eg. Status) I'm representing it as an enum in my program. But I'm using the string representation in my database. So its quite troublesome to convert from string to enum values. Usually I create a funciton that looks like this:
MyEnum Convert(string myValue) {
switch (myValue) {
case "item1":
return MyEnum.item1;
case "item2":
return MyEnum.item2;
}
}
As you can see its quite lame :). But I got used to it during the C++ days. Now, this is the exact problem that the Parse method solves. It reads a string and automatically converts it to the enum that you are using. Here's a sample:
retVal.PayMode = (PaymentMode) Enum.Parse(typeof(PaymentMode), reader["PayMode"].ToString(), true);
Now this sample reads a string in a DataReader object and let the Parse method convert it to its Enum (PaymentMode) representation. Now this is pretty useful if your using many enums in your program. If you want the reverse, convert an enum into its string representation then you can directly use the ToString() method.
The second useful function is the GetNames method. It's quite simple, it returns an Array of strings containing all of your enums in its string representation. This is quite useful if you want to bind a control to an enum. For example, you have a combo box control in which you want to display the contents of your enum for your user to choose from. Instead of doing it manually you can directly bind it this way:
comboBox1.DataSource = Enum.GetNames(typeof(MyEnum));
Also, take note that when using the enum methods you must use the Enum class which has an Upper Case E. Enum and enum in C# is quite different. So use the Enum if your trying access its methods, and use enum (with a small e) if your just declaring an enumeration.