April 21, 2004

  • More .NET Stupidities: Enum::Parse()

    Today I got totally pissed off by M$’s brilliant software
    engineering again, while working with enum objects in C++ .NET. If you
    are a C++ programmer, one of the things you must have encountered is
    the conversion/casting of a string to an enumerated type. In .NET, M$
    implements an absolutely stupid way of string-to-enum conversion,
    through the Enum::Parse() function. Basically, you expect to just pass
    the function a string and the custom enum type as parameters then get
    back an enum value in return. WRONG!!!

    3 Major Stupidities:

    1. You need to make sure your enum type is a garbage collected
    object, by specifying “__value” when you declared the enum type (e.g.
    “__value enum fruits {apple, orange, banana, 1337fruit};”

    2. You need to specify “__typeof” for your enum type in the argument
    list of Enum::Parse() because .NET is stupid and doesn’t realize that
    you’re trying to give it an enumerated type though you are using a
    function in the Enum namespace (duh!).

    3. Worst of all: the returned value of the Enum::Parse() function is
    not even going to be in your custom enum type, it’s the generic garbage
    collected Object*!!! You have to do “*dynamic_cast” like this:

    some_enum_type enum_var = *dynamic_cast<__box(some_enum_type)>(Enum::Parse(__typeof(some_enum_type), some_string));

    Now, if that is not retarded, I don’t know what is.

    - SwordAngel

Post a Comment

Leave a Reply

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