Sunday, November 30, 2014

Storing integers in an enum

Example
  
enum DepartmentID
    {
        HumanResource = 1001,
        Marketing = 1002,
        Sales = 1003,
        Logistics = 1004,
        Operations = 1005,
        Finance = 2003,
        IT = 2002,
        Training = 2001,      
        SharedServices = 1006,
    }

On instances where you need the departmental id, you could easily retrieve the integer values of the enum by casting:

Eg:
(int)DepartmentID.Marketing


Hence, the code becomes easy to maintain.
On a side note: Comes in handy when building a query string for a sql select command.


No comments:

Post a Comment