Getting Enums From Ints

posted on 04/19/07 at 12:04:01 pm by Joel Ross

On just about every project I've worked on, we end up having?an object that has a property that comes from a list of known values that don't change. Because of this, we end up with the data in two places stored in two different ways. First, it's in the database as a lookup table, mainly so we can ensure that we don't lose any of our data integrity. We also end up with an enumeration in our business classes, so we can program against the known values. As a result, we end up converting between that int an the enumeration quite a bit, and this quick helper method from Paul Wilson is a nice tool to have in my back pocket.

I can now see a time when I won't have to have an int property in my objects if it's a lookup value - it can be an enum and just convert as it's loaded.

By the way, another way I've solved this in the past is to not use an enum, and instead use a set of constant ints that represent my lookup values. This is actually my preferred method of handling this right now - even considering this helper method.

Technorati Tags:

Categories: C#