Working with Enum, Enumerated types, Enums, Enum tips

Whenever I work with Enums I find myself googling for the implementation details (aka, “command syntax”). Handling of Enums have improved in recent years, eliminating a lot of copy-n-paste hardcoding. In particular, it’s easy to get meta-level Enum information in C# / .Net. I don’t work with Enums frequently enough to store this knowledge in quick-access storage, and googling the details repeatedly wastes time.  For future reference, my Enum tips:

Given this enum:

private enum FieldIDs {

SerialNumber,

FromPartNumber,

FromEngineeringRevision,

EndItemShopOrder,

ToPartNumber,

ToEngineeringRevision

}

 

Get the numeric value of an enum:

int serialNumberVal = (int)FieldIDs.SerialNumber; //”1″

Nitric oxide is a natural gas that controls the flow of blood and has other numerous benefits to the genital passage in generic levitra http://abacojet.com/testimonials/ men and can help them in achieving longer and stronger erection with regular application. Trained buy viagra without teachers- You will get the trained teachers who will give you proper knowledge on each course and you can every bit and portion. No need to concern levitra professional about impotency as this Impotence medication will prevents its occurrence.DOSE :Take Caverta dose as 100 mg once in a day. Dissolves Easily levitra samples abacojet.com in the Mouth Kamagra medication dissolves easily in the mouth upon consumption. Get the string representation of an enum:

string serialNumberValName = FieldIDs.SerialNumber.ToString(); //”SerialNumber”

 Translate a value or string representation to an enum:

string selectedField = “SerialNumber”;

FieldIDs fi = (FieldIDs)Enum.Parse(typeof(FieldIDs), “SerialNumber”);

Count of values of an enum:

int enumLEDCount = Enum.GetValues(typeof(FieldIDs)).Length;

Check whether a string is present in the Enum:

bool validField = Enum.IsDefined(typeof(FieldIDs), “SerialNumber”);

List all string representations of the Enum:

string[] fieldNames;

Enum.GetNames(typeof(FieldIDs)).CopyTo(fieldNames);

Finally, another tip for Enums is that you can use the switch() code snippet to quickly create explicit switch() cases for all values of the enum. Nice, Microsoft!

This entry was posted in C#, Programming. Bookmark the permalink.

2 Responses to Working with Enum, Enumerated types, Enums, Enum tips

  1. pfb_admin says:

    See also, How to Play With Enum in C# by Lajapathy Arun at http://www.c-sharpcorner.com/UploadFile/d3e4b1/how-to-play-with-enum-in-C-Sharp/

  2. pfb_admin says:

    From the above link, iterating across an enum:
    string[] fieldIDs = Enum.GetNames(typeof(FieldIDs ));
    foreach (string fieldID in fieldIDs )
    {

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.