Enum values and names

Posted by jmls on 19-Sep-2011 07:05

I've been reading up on the enum patterns from various people (autoedge, consultingwerk, etc) and have a question.

There are plenty of examples of how to get an enum object from a name "ordered", "shipped" etc (giving me 0, 1 etc)

However, when persisting the data, I presume that we store the value of the enum  in the database (0, 1) . Now, how does one go about converting the value back into an Enum object?

I've written a method called FromValue that does a case on all the appropriate values (just like the FromString() methods I've seen) but was wondering why the functionality of this method isn't present in the classes I've seen.

I'm obviously missing something important

All Replies

Posted by Admin on 19-Sep-2011 07:11

I've written a method called FromValue that does a case on all the appropriate values (just like the FromString() methods I've seen) but was wondering why the functionality of this method isn't present in the classes I've seen.

 

Speaking for the Consultingwerk implementation (http://blog.consultingwerk.de/consultingwerkblog/2010/08/a-pattern-for-enums/) here.

No all Enums need to be able to get persisted in a DB - so it's not a core requirement. ToString() for value to string is simple and typically used for logging/debugging purposes.

When we need string to value conversion we implement that on a case by case basis, typically using a CASE statement.

For a dynamic//generic ToValue() implementation I'm waiting for a future OpenEdge Release to allow me dynamic access to properties. That's simple not possible in 10.2B.

Posted by Peter Judge on 20-Sep-2011 10:14

jmls wrote:

I've been reading up on the enum patterns from various people (autoedge, consultingwerk, etc) and have a question.

There are plenty of examples of how to get an enum object from a name "ordered", "shipped" etc (giving me 0, 1 etc)

However, when persisting the data, I presume that we store the value of the enum  in the database (0, 1) . Now, how does one go about converting the value back into an Enum object?

I've written a method called FromValue that does a case on all the appropriate values (just like the FromString() methods I've seen) but was wondering why the functionality of this method isn't present in the classes I've seen.

I'm obviously missing something important

And speaking from the AutoEdge side - javascript:; - we do pretty much the same thing as Mike. Not all enums need conversions/serialisation, and so we also have methods to converts to and from string and/or integer values to an Enum object (we call 'em EnumFromString() and EnumFromValue() ).

However Mike's use of the word "serialise" triggers a refactoring thought: we already have support for serialising enum's in our serialisation protocol, so why do we need a separate method. Thinks ...

-- peter

Posted by bdearborn on 20-Sep-2011 10:16

Hi,

I am currently out of the office on business. I will be checking my

email at least a couple of times per day but if you need immediate

assistance please call my cell at 503-805-8598. I will be returning to

the office on September 23, 2011. You can also contact Jim Stein at

503-928-8112 or email him at jstein@cornerstar.com with any questions.

Thanks,

Bryn Dearborn

503-805-8598

--

Bryn Dearborn

503-928-8115 (Work)

503-805-8598 (Cell)

Cornerstar

This thread is closed