A better way to parse enums



  • We all know and love the methods that the .net framework provides for working with enumerations... things like Enum.Parse(), etc etc. While traversing through some of our production code I stumbled across this gem. Its parsing methods far surpass anything that the Enum.Parse() could ever dream of. Knowing the previous developer has a knack for the desire to write his own snippets of code (aka: reinventing the wheel) this came as no surprise:

    <FONT color=#008080 size=2><FONT size=2>

    </FONT><FONT color=#0000ff size=2>private</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>string</FONT><FONT size=2> Guess(</FONT><FONT color=#0000ff size=2>string</FONT><FONT size=2> str)

    {

    </FONT><FONT color=#0000ff size=2>    switch</FONT><FONT size=2> (</FONT><FONT color=#0000ff size=2>char</FONT><FONT size=2>.ToUpper(str[0]))</FONT>

    <FONT size=2> {</FONT>

    <FONT color=#0000ff size=2>c</FONT><FONT color=#0000ff size=2>ase</FONT><FONT size=2> </FONT><FONT color=#800000 size=2>'B'</FONT><FONT size=2>:

    </FONT><FONT color=#0000ff size=2>return</FONT><FONT size=2> </FONT><FONT color=#008080 size=2>MyEnum</FONT><FONT size=2>.Between.ToString();

    </FONT><FONT color=#0000ff size=2>case</FONT><FONT size=2> </FONT><FONT color=#800000 size=2>'O'</FONT><FONT size=2>:

    </FONT><FONT color=#0000ff size=2>return</FONT><FONT size=2> </FONT><FONT color=#008080 size=2>MyEnum</FONT><FONT size=2>.Outside.ToString();

    </FONT><FONT color=#0000ff size=2>case</FONT><FONT size=2> </FONT><FONT color=#800000 size=2>'E'</FONT><FONT size=2>:

    </FONT><FONT color=#0000ff size=2>return</FONT><FONT size=2> </FONT><FONT color=#008080 size=2>MyEnum</FONT><FONT size=2>.Equal.ToString();

    </FONT><FONT color=#0000ff size=2>case</FONT><FONT size=2> </FONT><FONT color=#800000 size=2>'L'</FONT><FONT size=2>:

    </FONT><FONT color=#0000ff size=2>return</FONT><FONT size=2> </FONT><FONT color=#008080 size=2>MyEnum</FONT><FONT size=2>.LessThan.ToString();

    </FONT><FONT color=#0000ff size=2>case</FONT><FONT size=2> </FONT><FONT color=#800000 size=2>'G'</FONT><FONT size=2>:

    </FONT><FONT color=#0000ff size=2>return</FONT><FONT size=2> </FONT><FONT color=#008080 size=2>MyEnum</FONT><FONT size=2>.GreaterThan.ToString();

    </FONT><FONT color=#0000ff size=2>default</FONT><FONT size=2>:

    </FONT><FONT color=#0000ff size=2>return</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>null</FONT><FONT size=2>;

    }

    }

    </FONT></FONT><FONT size=2></FONT>


  • It appears to do a little more than just parse an Enum, as its return value is a string. However, what is an expected input to this function? And a lovely name for the function by the way.



  • The funny thing about it... it is parsing a field from a file and then passing that entry into the Guess method...

    so its taking a string.... comparing that to the first character and if it matches then it returns the enumeration value ToString()... the WTF is that we are limited to a certain number of enumeration values.

     So we can't use Between and Both or Boseefus as it will always guess that since it starts with 'B' then it MUST be Between.



  • @bradfoje said:

    We all know and love the methods that the .net framework provides for working with enumerations

    I assure you I don't :(



  • @rbowes said:

    @bradfoje said:

    We all know and love the methods that the .net framework provides for working with enumerations

    I assure you I don't :(

    When I was doing a lot of interviewing, we had people fill out a 'self assessment' and rate themselves in several areas.   These rankings even had rough descriptions.  1 = "I've heard of it and read a bit but I haven't used it on a project yet".   3 = "I've worked with this for a while and am comfortable in it".   5 = "I am a world-class expert and could write books on the topic".    When I conducted the interview, I came in with questions keyed to those levels.

    It's was WTFy enough that about 60% of respondants rated themselves as being a 5 in .NET (and several other technologies) after 3 years experience. But not a single one of them knew Enum.Parse().  Not one.     So you're not alone :)

    Generally speaking, they also didn't know what .Dispose() was for...and god help you if you asked them what the relationship between the CLR & a Windows process was. 

    (I also got a contractor sent to me with several years experience who, when I asked him to write me a function to clean up a phone number, looked at it for a few seconds and said "Pass"...sigh)

    It was a long couple months.

    Now I'm depressed.

    -cw



  • @CodeWhisperer said:

    When I was doing a lot of interviewing, ...

    It was a long couple months.

    Now I'm depressed.

    -cw

    Hmm... now you sound like CPound. I start to believe that the four of us are really only one person with a split personality!



  • No, if I was CPound, I'd have complained that they wore tennis shoes and needed haircuts.  I actually care about what candidates know :)

    Ooo...though I guess it's possible that I'm the personality that cares about such things.

    -cw



  • @CodeWhisperer said:

    When I was doing a lot of interviewing, we had people fill out a 'self assessment' and rate themselves in several areas.   These rankings even had rough descriptions.  1 = "I've heard of it and read a bit but I haven't used it on a project yet".   3 = "I've worked with this for a while and am comfortable in it".   5 = "I am a world-class expert and could write books on the topic".    When I conducted the interview, I came in with questions keyed to those levels.

    It's was WTFy enough that about 60% of respondants rated themselves as being a 5 in .NET (and several other technologies) after 3 years experience. But not a single one of them knew Enum.Parse().  Not one.     So you're not alone :)

    Well, that makes me feel better. I would rate myself as a "1" or "2" on .net -- I've heard about it, and I know a few key concepts (or can guess based on my Java skills). And I can take a pretty good guess on what Enum.parse() and .Dispose() do. But the original poster made it sound like something totally obvious that all of us know, but I am reasonably sure that those of us who have never done .net wouldn't. :)


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.