• (cs) in reply to Matt
    AdT:
    Not seen in this example are the functions that convert an EmailFormat enum to an EmailFormats enum, an EmailsFormat enum to an EmailFormats enum and both EmailsFormat and EmailFormats enums to EmailsFormats enums and vice versa, and those that convert arrays of one to arrays of another.
    Yeah, and where's the update on Britney's latest adventures?
    Matt:
    Why are the valid values for the Enum hard-coded. They should be queried from a database and loaded into an array. Then, if your valid values change, you don't have to change your code.
    No, they should be stored as hardcopy in a paper stored in a file cabinet. Then if your valid values change you can take out the paper, write in your changes, put it on a wooden table, take a polaroid of it, run the picture through a scanner with OCR software, and process the result into your application.
  • Jeffrey L. Whitledge (unregistered)

    In the Framework Design Guidelines section 5.7.2: “Do not use Enum.IsDefined for enum range checks.”

    In the annotated version, Brad Abrams then gives a lengthy discussion about why you should check each value instead of depending on Enum.IsDefined.

    There is nothing wrong with the code presented.

  • Matt (unregistered) in reply to FredSaw
    FredSaw:
    Matt:
    Why are the valid values for the Enum hard-coded. They should be queried from a database and loaded into an array. Then, if your valid values change, you don't have to change your code.
    No, they should be stored as hardcopy in a paper stored in a file cabinet. Then if your valid values change you can take out the paper, write in your changes, put it on a wooden table, take a polaroid of it, run the picture through a scanner with OCR software, and process the result into your application.

    Ah- you're thinking of Enterprise-level data persistence. In that case, for best results, write the correct results to a hardcopy XML file. That way, it's human-readable, too. There are those who would write the values in JSON notation, but there will always be those who go overboard.

  • eff Five (unregistered) in reply to Jeffrey L. Whitledge
    Jeffrey L. Whitledge:
    In the Framework Design Guidelines section 5.7.2: “Do not use Enum.IsDefined for enum range checks.”

    In the annotated version, Brad Abrams then gives a lengthy discussion about why you should check each value instead of depending on Enum.IsDefined.

    There is nothing wrong with the code presented.

    Except of course that the code throws System.Exception which is a no-no according in section 7.3.1 of said Guideline.

    Also the content to the annotation you reference can be found http://blogs.msdn.com/brada/archive/2003/11/29/50903.aspx which I referenced in an earlier comment.

    -Pedantic Joe

  • (cs)

    Wow. I know that the comments here are usually pretty silly, but some of today's really are terrible. Let's recap .NET 101 here:

    1. This code is doing type conversion, not range checking. The types appear to be identical, but we don't know that. The EmailFormat enum might contain HTML = 3 and Text = 4 along with a few other formats, which would make type conversion based on direct casts incorrect.

    2. The switch construct here MUST have a default case, otherwise it would produce a compiler error.

    3. Enums in C# let you specify the values. This has many uses, including the FlagsAttribute as well as the ability to wrap some database column with an enum or support a direct cast to a different type. It also means that automatic range checking would be abominably slow for large enums (and there are several).

    4. For the geek who posted the C++ code with a constructor and operator overload, an Enum in C# isn't a struct. It doesn't have constructors, and it doesn't have operators. That's precisely why you'd write a conversion method in an actual class (this one's even declared static!).

    5. Enums in C# are far more flexible and easier to use than the retarded BitSet in Java. I'm not going to expand on that because it really ought to be obvious to anyone who's actually had experience with both languages (and if you haven't - keep quiet!)

    Point (2) is really the most important here. C# doesn't actually do static analysis on your code in order to verify that you've covered every possibility (FYI, neither does Java). It makes certain assumptions. For example, if you write this useless bit of code:

    private bool Test(object o)
    {
        if (o == null)
        {
            return true;
        }
        else if (o != null)
        {
            return false;
        }
    }
    

    It won't compile. You'll get a "not all code paths return a value" error. It's pretty obvious to a programmer that all code paths do, in fact, return a value. "o" is either null or not null. But the C# compiler just sees that you have an if/else-if without an else and without a subsequent return. If you don't evaluate each and every conditional against the entire scope of possible conditions (and it's practically impossible for a compiler to do this in reasonable time), then it looks like the return value might actually be undefined.

    Same goes with switch/case constructs. Even if you've covered every possible enum value, the compiler can't know this before the code is actually compiled! So it insists on either a default case, or a return/throw statement after the switch. In this case, the programmer has chosen to throw rather than return a default value. He knows that branch should never be executed! It's essentially a choice between an arbitrary return value and a meaningless exception.

    Having said that, there is a convention for this that makes a little more sense. What you're supposed to do is:

    switch (format)
    {
        case EmailsFormat.HTML:
            return EmailFormat.HTML;
        default:
            Debug.Assert(format == EmailsFormat.Text);
            return EmailFormat.Text;
    }

    This code is much easier to maintain because it explicitly acknowledges that "Text" should be the only remaining option after all others have been tried. If another programmer adds a new Enum type (say EmailsFormat.RichText) and forgets to add that to the conversion function, this will fail in debug mode but use a default value in release mode. That may or may not be what you really want, but it's generally better than a runtime error.

    If I really need something like that to produce a runtime error, i.e. because using a default value could corrupt other data, then I usually use the NotImplementedException. If I ever get a bug report with that (and I never have), it tells me "Whoops, you changed something but forgot to change this other part that depends on it!" This is rare, though; usually the Assert behaviour is preferable.

  • Jon W (unregistered)

    I think the code is actually:

    1. The best way to convert between two enum types.
    • Going through strings isn't only a LOT slower, it's also less robust, especially when possible future enum values won't have the exact same spellings.
    1. Robust, in that it signals an exception as soon as it finds something unexpected.

    I like it. I would have written something similar myself, if I had to convert enums from one library convention to another. This has got to be the least WTF-ey code ever posted on WTF.

    So, the REAL WTF is that Alex has no QA on the submissions. I bet he didn't spot the Email/Emails difference when reading (which I did on the first try, for some reason, thus having to look hard for any WTF-ery).

  • (cs) in reply to Hench
    Hench:
    Actually, you're wrong. Having a "throws Exception" in the method header only applies if some code other than a "throw" command throws an Exception.

    I don't understand that sentence at all.

  • Billy Bob (unregistered)

    Speaking of pedantic guideline adherance, "HTML" should be "Html"

  • OMG (unregistered) in reply to Hench
    Hench:
    Actually, you're wrong. Having a "throws Exception" in the method header only applies if some code other than a "throw" command throws an Exception. It's used mainly in place of try-catch blocks. So yes, that's valid Java.

    All I can say is OMG WTF.

  • someguy (unregistered)
    public static boolean fromBoolean(Boolean bool) throws FileNotFoundException{
      if (bool.booleanValue() == true) {
        return true;
      } else if (boolean.booleanValue() == false) {
        return false;
      } else throw new FileNotFoundException();
    }
  • someguy (unregistered) in reply to Licky Lindsay
    Licky Lindsay:
    Hench:
    Actually, you're wrong. Having a "throws Exception" in the method header only applies if some code other than a "throw" command throws an Exception.

    I don't understand that sentence at all.

    What's not to understand? It's a very clear sentence. It's also very incorrect.

  • Trantor (unregistered) in reply to IceFreak2000
    IceFreak2000:
    In which case, the function could be reduced to
    return (EmailFormat)Enum.Parse(typeof(EmailFormat), Enum.GetName(typeof(EmailsFormat), emailsFormat);
    assuming of course that there was 1-to-1 parity with the defined names in the enumerations.

    Even if there is a 1-to-1 correspondence, this is a really bad idea. The problem is that if someone decides to rename the enumeration values (because of new code style standards, for example), the "optimized" code will still compile, but it won't work anymore. If that section of code isn't used very often, the problem might not be noticed until a customer runs into it. But the line-by-line conversions will cause a compiler error. (They might even be renamed automatically, depending upon where they are in the solution.)

  • (cs) in reply to Bart
    Bart:
    Enum.IsDefined() was introduced in .NET 2.0. This code could have been written against the 1.1 framework.

    Have you actually read the documentation? It clearly states that IsDefined is supported all the way back to 1.0.

  • knock it off... (unregistered) in reply to Hench
    Hench:
    Biffa:
    As an aside, the code in the snippet actually is valid in Java
    Seeming as I'm in an incredibly pedantic mood today - no it's not.

    If it were java it would have to have a "throws Exception" clause in the method signature...

    Actually, you're wrong. Having a "throws Exception" in the method header only applies if some code other than a "throw" command throws an Exception. It's used mainly in place of try-catch blocks. So yes, that's valid Java.

    Absolute and utter rubbish!

    Biffa is absolutely right. There's no difference between "some other code than a 'throw' command" and a "throw command". By "some other code" you obviously mean a call to method throwing an Exception - which is in fact nothing else than a method with a "throws"-clause. How do you think the exception is thrown in that "other" code? Yes, you guessed it (or perhaps not): by a "throw" statement...

    This is in fact quite close to valid Java - if you'd run this through the Java-compiler, you'd get basically 2 errors:

    1. The enum-constants in a "case"-label MUST NOT be qualified (a design choice which one might like or dislike, but that's how it is)
    2. The Exception is unhandled

    There are also two breaches of naming conventions: The enum-constants should both be uppercase ("TEXT") and the first character of the method name should be lowercase - there's a reason why types (classes, interfaces, enums and annotations) are the only constructs written with an uppercase first letter in Java! Nevertheless, this misnomers wouldn't faze the compiler.

    To make this correct Java, you'd write:

    public enum EmailsFormat { HTML, TEXT }
    
    public static EmailFormat fromEmailsFormat(EmailsFormat emailsFormat) 
            throws Exception
    	{
    	    switch(emailsFormat)
    	    {
    	        case HTML:
    	            return EmailFormat.HTML;
    	        case TEXT:
    	            return EmailFormat.TEXT;
    	        default:
    	            throw new Exception( "Unknown EmailsFormat enum value!" );
    	    }
    }
    

    If you don't like having to declare the exception in the method signature, leave out the "throws"-clause and use

    throw new RuntimeException("...");

    instead. The semantics change slighty then - the caller of the method is not required to handle the Exception in the latter case.

    Otherwise (in presence of "throws Exception") he is facing the choice of either using a "try/catch" block around the call or declaring "throws Exception" himself and thereby letting the Exception propagate further up the call stack.

    But in fact the best practice when dealing with unknown values in a "switch/case" statement is to use

    default: throw new AssertionError("Unknown Value");

    which also does not have to be declared in the "throws"-clause (so you can leave that out again) and was coined specifically for cases like this.

    You're welcome to try both the original and the above version and see for yourself :oP

  • nils (unregistered) in reply to rdrunner
    rdrunner:

    Just imagine EmailFormat to be defined like this:

    public enum EmailFormat { Plain, ExtraFunky, HTML, Text }

    And this makes sense... Well except for the funky emails ;)

    100% agree - checks like that make code solid.

    If you write a library that others use and you want to check input parameters you have to check enum-parameters like this. Unfortunately you can't do something like ((a >= first-enun) && (a<= last_enum))

    That might work in java (don't know ... ) but in C and C++ you can define enums with gaps in it, and you can pass ints to functions expecting enums without warnings.

  • Hench (unregistered) in reply to knock it off...

    I've been soundly proven wrong.

    It was my error; I was clearly lied to by various sources.

    My apologies.

  • Hey (unregistered)

    first time "not a WTF" comes to my mind, it looks like a good way to prevent you from updating the code correctly later, if you add a new value to the enum.

  • MountainRider (unregistered)

    If that is C++ code, someone could cast any value to the parameter for that function, so it is possible for that function to get an invalid value. I would argue that throwing an exception is not the most gentle way to handle the error.

    Captcha: gygax (Gary Gygax? The D&D guy?)

  • (cs) in reply to eff Five
    eff Five:
    Jeffrey L. Whitledge:
    In the Framework Design Guidelines section 5.7.2: “Do not use Enum.IsDefined for enum range checks.”

    In the annotated version, Brad Abrams then gives a lengthy discussion about why you should check each value instead of depending on Enum.IsDefined.

    There is nothing wrong with the code presented.

    Except of course that the code throws System.Exception which is a no-no according in section 7.3.1 of said Guideline.

    Also the content to the annotation you reference can be found http://blogs.msdn.com/brada/archive/2003/11/29/50903.aspx which I referenced in an earlier comment.

    -Pedantic Joe

    True (or maybe not).

    I'm prepared to accept your analysis. I've never met you. I've never had to write this sort of code in, say, C#. I can, however, look it up o the web if needs be.

    I do find it difficult to deal with comments such as "There is nothing wrong with the code presented,"

    In a sense, this is true. It will compile. It is almost self-documenting, in a way, It will probably (in a static environment) do its job.

    However, it seems to be a classic case of what Alex is forced by Grandmother Demand to call "Worse Than Failure."

    If you really, really, think that there is nothing wrong with the code as presented, then please think again. Perhaps there are two or three subtleties that you'd like to revisit.

  • (cs) in reply to real_aardvark
    real_aardvark:
    eff Five:
    Jeffrey L. Whitledge:
    In the Framework Design Guidelines section 5.7.2: “Do not use Enum.IsDefined for enum range checks.”

    In the annotated version, Brad Abrams then gives a lengthy discussion about why you should check each value instead of depending on Enum.IsDefined.

    There is nothing wrong with the code presented.

    Except of course that the code throws System.Exception which is a no-no according in section 7.3.1 of said Guideline.

    Also the content to the annotation you reference can be found http://blogs.msdn.com/brada/archive/2003/11/29/50903.aspx which I referenced in an earlier comment.

    -Pedantic Joe

    True (or maybe not).

    I'm prepared to accept your analysis. I've never met you. I've never had to write this sort of code in, say, C#. I can, however, look it up o the web if needs be.

    I do find it difficult to deal with comments such as "There is nothing wrong with the code presented,"

    In a sense, this is true. It will compile. It is almost self-documenting, in a way, It will probably (in a static environment) do its job.

    However, it seems to be a classic case of what Alex is forced by Grandmother Demand to call "Worse Than Failure."

    If you really, really, think that there is nothing wrong with the code as presented, then please think again. Perhaps there are two or three subtleties that you'd like to revisit.

    Buggered by the five minute limit again.

    Eff Five: The "you" here is mis-directed. Jeffrey L. Whitledge: What's the matter, sweetheart? Lost control of the trust fund and had to make your way in the real world?

    If so, I'd try harder. The "you" above specifically refers to you. Personally.

    No, really, I've just checked. Apparently, you don't have a clue.

  • Geo (unregistered)

    I agree that this is a bogus WTF. Moreover, it's a WTF lampooning what amounts to excellent coding practice. I always use the default in a switch statement to throw an "invalid value" exception (assuming that's an error condition, of course).

    It's only a couple of extra lines of code, and it guards against real maintenance issues, the most obvious of which is adding new enum values after the code is written.

    IsDefined() is great, but only gets you as far as making sure it's a real enum before switching on it. What you're guarding against is a new, valid value falling silently through the switch, and IsDefined won't do jack for that.

  • Stefan W. (unregistered)

    If it was Java, you would: a) use a lowerCaseMethodName b) omit the needless 'default', because nothing else is possible c) therefore not need a throws-statement d) write TEXT in UPPERCASE, because it's a kind of constant e) -- not mentioned before --: Move the method into the class, where it belongs..

    public enum EmailsFormat
    {
    	HTML, TEXT;
    	// note, we return emailSformat
    	public EmailFormat toEmailFormat ()
    	{
    		switch (this)
    		{
    			case HTML:
    				return EmailFormat.HTML;
    			case TEXT:
    				return EmailFormat.Text;
    		}
    	}
    }

    And of course give a hint by a (more pretty than mine) comment, that we're not morons, but do useful work.

    This is much better readable, and perhaps we would like to avoid the depedency SFormat -> _Format and leave it somewhere else.

    Perhaps convert it in the EmailFormat-Enum? And just call it 'convert':

     public enum EmailFormat
    {
    	HTML, Text;
    	static public EmailFormat convert (EmailsFormat sFormat)
    	{
    		switch (sFormat)
    		{
    			case HTML: 
    				return HTML;
    			default: 
    				return Text;
    		}
    	}
    } 

    But it's C# - what a pity.

  • Adam (unregistered)

    Perhaps EmailFormat is not an enum. Maybe it's a class and HTML is a well known instance of the class. Like the UTF8 conversion class in the .NET framework.

  • hax (unregistered)

    This reminds me of something I found in one of my first VB programs:

    if foo = true then 'something else if foo = false then 'something else msgbox "omfg haxor alert" end end if

    foo, of course, being Boolean. The captcha, "craaazy", describes this well.

  • Raw (unregistered)

    Could be a leftover from another language. For instance, early VB versions did not enforce enums, they were just a way to put names on the alternatives. For instance, the following was perfectly legal:

    Enum Color Red=0 Green=1 Blue=2 End Enum

    Dim Col as Color

    Col=Color.Red 'OK Col=3 'Also OK

  • (cs)

    Ha! He doesn't for a null value. He loses.

  • (cs) in reply to Hench
    Hench:
    Actually, you're wrong. Having a "throws Exception" in the method header only applies if some code other than a "throw" command throws an Exception. It's used mainly in place of try-catch blocks. So yes, that's valid Java.
    $ cat WTF.java
    class WTF
    {
            void Hench()
            {
                    throw new Exception();
            }
    }
    bash$ javac WTF.java
    WTF.java:5: unreported exception java.lang.Exception; must be caught or declared to be thrown
                    throw new Exception();
                    ^
    1 error
    bash$ javac -version
    javac 1.6.0
    

    I think you were thinking about RuntimeException and it's derivatives; they don't ever need to be declared as thrown.

    bash$ cat WTF.java
    class WTF
    {
            void Hench()
            {
                    throw new RuntimeException();
            }
    }
    bash$ javac WTF.java
    bash$
    
  • leppie (unregistered)

    The submitter's WTF due to lack of knowledge...

  • (cs) in reply to Hench
    Hench:
    I've been soundly proven wrong.

    It was my error; I was clearly lied to by various sources.

    My apologies.

    And I should really read the whole thread before replying; sorry about repeating the correction after you'd acknowledged it.

  • (cs) in reply to Adam
    Adam:
    Perhaps EmailFormat is not an enum. Maybe it's a class and HTML is a well known instance of the class. Like the UTF8 conversion class in the .NET framework.

    That's more or less what all enums are in Java. The enum keyword was added to the language so that you would no longer need to write stuff like this:

    public class Color {

     private String name;
    
     //private so that only I can me instances of me
     private Color(String s) { name = s; }
    
     public String toString() { return name; }
    
     public final static Color RED = new Color("RED");
     public final static Color BLUE = new Color("BLUE");
     public final static Color GREEN = new Color("GREEN");
    

    }

  • Sérgio (unregistered)

    This is called Hyper Secure Redundant Programming, HSRP.

  • Jeffrey L. Whitledge (unregistered) in reply to real_aardvark

    I stand by my earlier statement. There is nothing wrong in the code presented.

    It is a platonic ideal like a perfect circle.

    In the world of all possible computer code in all languages, this represents not only a pinnacle of achievement, but the embodiment of absolute good.

    Every day I get down on my hand and knees and pray that my code can only be a tenth as good as the example that was presented.

    That was obviously what I meant the first time, and I stand by it now, and always will.

  • (cs) in reply to ahnfelt
    ahnfelt:
    Great idea, since
    return (EmailFormat)Enum.Parse(typeof(EmailFormat), Enum.GetName(typeof(EmailsFormat), emailsFormat);
    is much better than
    return FromEmailFormat(emailsFormat);
    Come on, this is not a WTF at all.

    But it is a WTF that C# doesn't guarantee that enums only hold valid values. What's the point? That's like having a boolean that sometimes be a string.

    You must be new here. What you should have said was: "That's like having a boolean that can sometimes be a file not found."

  • (cs) in reply to Fj
    Fj:
    ahnfelt:
    But it is a WTF that C# doesn't guarantee that enums only hold valid values. What's the point?

    This way you can have

    [Flags]
    enum SomeFlags
    {
      None,
      Flag1 = 0x100,
      Flag2 = 0x200,
      Flag3 = 0x400,
      FlagValueMask = 0xFF;
    }
    
    ...
    
    SomeFlags zzz = SomeFlags.Flag1 | SomeFlags.Flag2 | (SomeFlags)42;
    

    which is kinda useful sometimes and would be impossible or incur terrible performance penalty if range checks are compulsory.

    Um, but that's stupid for an OO language.

    class SomeFlags { public boolean flag1, flag2, flag3; public UInt8 value; }

  • knock it off... (unregistered) in reply to Stefan W.
    Stefan W.:
    If it was Java, you would: a) use a lowerCaseMethodName b) omit the needless 'default', because nothing else is possible c) therefore not need a throws-statement d) write TEXT in UPPERCASE, because it's a kind of constant

    Just what I already said above ;o) And you have a valid point with e) (though it's not really necessary)

    But I don't agree with b) and c).

    Best practice is (as I also said above):

    switch(emailsFormat)
    {
        case HTML:
            return EmailFormat.HTML;
        case TEXT:
            return EmailFormat.TEXT;
        default:
            throw new AssertionError(emailsFormat);
    }
    

    You are right when you state that no other values are possible.

    Still, someone might add another constant and forget to update/recompile all occurrences of such switch-statements. Unfortunately, you won't notice that immediately and/or will likely get strange behaviour (depending on what you're doing in the case-branches). That's why it is best practice to throw an AssertionError, because it is a) unchecked b) not meant to be caught, because it is a subclass of Error and therefore c) perfect for asserting that no constant has been forgotten in the switch (now or in the future!)

  • (cs) in reply to Raw
    Raw:
    Could be a leftover from another language. For instance, early VB versions did not enforce enums, they were just a way to put names on the alternatives. For instance, the following was perfectly legal:

    Enum Color Red=0 Green=1 Blue=2 End Enum

    Dim Col as Color

    Col=Color.Red 'OK Col=3 'Also OK

    Yes, well, obviously it's from another language. In this case (I would hazard a guess) C++, although C is also a reasonable assumption.

    Nice to know that I'm a geek, though. I presented a binary choice: (1) One: "rely on the language" -- hopefully .NET post 1.1 (2) Courtesy of http://en.wikipedia.org/wiki/Niklaus_Wirth, algorithms + data = programs.

    The fact that I wrote the example in C++ doesn't make me a geek. It just suggests that I'm ignorant in terms of C# or Java.

    What, exactly, should I write an example in? VB4? (Shudder...)

  • Synonymous Awkward (unregistered) in reply to Fj
    Fj:
    [Flags]
    enum SomeFlags
    {
      None,
      Flag1 = 0x100,
      Flag2 = 0x200,
      Flag3 = 0x400,
      FlagValueMask = 0xFF;
    }
    

    ...

    SomeFlags zzz = SomeFlags.Flag1 | SomeFlags.Flag2 | (SomeFlags)42;

    which is kinda useful sometimes and would be impossible or incur terrible performance penalty if range checks are compulsory.

    Only problem with that enum is that it doesn't enumerate anything; it's a bitfield.

    Pl... please tell me C# can handle bitfields?

  • BG (unregistered) in reply to knock it off...
    public enum EmailsFormat { HTML, TEXT }
    

    public static EmailFormat fromEmailsFormat(EmailsFormat emailsFormat) throws Exception { switch(emailsFormat) { case HTML: return EmailFormat.HTML; case TEXT: return EmailFormat.TEXT; default: throw new Exception( "Unknown EmailsFormat enum value!" ); } }

    If I were code-reviewing that method, I would reject it for the fact that there are multiple returns in the method body (which I loathe). I would actually rewrite the method to be similar to:

    public static EmailFormat fromEmailsFormat(EmailsFormat emailsFormat) throws Exception
    {
        final EmailFormat result;
        switch(emailsFormat)
        {
            case HTML:
                result = EmailFormat.HTML;
                break;
            case TEXT:
                result = EmailFormat.TEXT;
                break;
            default:
                throw new Exception( "Unknown EmailsFormat enum value!" );               
        }
        return result;
    }

    This type of defensive coding protects against all the nasty things that can happen in switch{} expressions: 1.) accidental fall through in the cases (by missing a break statement) 2.) the default case is always handled by forcing the compiler to do the checking.

  • Stefan W. (unregistered)

    how can you accidential fail through, if you return immediately? Which default-case for email?

    No one suffering on compulsion neurosis would review my code.

    But here is still a better approach:

     public enum EmailsFormat
    {
    	HTML { EmailFormat toEmailFormat () { return EmailFormat.HTML; }},
    	TEXT { EmailFormat toEmailFormat () { return EmailFormat.Text; }};
    	abstract EmailFormat toEmailFormat ();
    } 
  • Blaufish (unregistered) in reply to ahnfelt

    Regarding "like having a boolean sometimes be a string".

    Hmm. I think you can, if you really try hard, make boolean behave strange in many languages.

    int main(int argc, char *argv[]) { bool value = true; bool *ptr_b = &value; char *ptr_c = (char *) ptr_b;

    *ptr_c = 2;
    if ( value == true ) printf("true");
    if ( value == false ) printf("false");
    printf( (value)?"evaluates to true\n":"evaluates to false\n");
    

    }

    Consider the wide range of possibilities :-)

  • ounos (unregistered)

    The real WTF is that the submitter thought that this is a WTF.

  • (cs)

    hey guy! do you really know what is Enum? I don't think so...

    I know it at http://www.enumobile.com and you can get your eNum here for FREE!

  • TraumaPony (unregistered) in reply to snoofle
    snoofle:
    ahnfelt:
    Great idea, since
    return (EmailFormat)Enum.Parse(typeof(EmailFormat), Enum.GetName(typeof(EmailsFormat), emailsFormat);
    is much better than
    return FromEmailFormat(emailsFormat);
    Come on, this is not a WTF at all.

    But it is a WTF that C# doesn't guarantee that enums only hold valid values. What's the point? That's like having a boolean that sometimes be a string.

    You mean like: enum Bool { TRUE,FALSE,FILE_NOT_FOUND };

    AHAHAHAHHAHAHAHAHHA

    OH MY GOD THAT'S SO FUNNY! AHAHAH!!!!

    ...

    Stop beating a dead horse.

  • dkf (unregistered) in reply to ounos
    ounos:
    The real WTF is that the submitter thought that this is a WTF.
    Oh, but it is. But the WTF is that the overall system has multiple enumerations for the same thing, opening the door to failures as things become silently out of synch. (Supporting only two types of email message is probably also an independent WTF...)
  • Synonymous Awkward (unregistered) in reply to TraumaPony
    TraumaPony:
    Stop beating a dead horse.

    I'd rather beat a TraumaPony.

  • Arioch (unregistered) in reply to Matt

    Sure, and the actual code (including that, to handle those types of e-mails) is also to be stored in database, for ease of upgrade.

    When the user wants ti run the program, he just launches MS Access, connects to SQL server, issues a needed EXECUTE PROCEDURE, then SQL server spits out needed code packases and runs them into JVM or CLR.

    Database is a ways t othe future, anything that is not stored inside SQL database is jast a worthless crap!

  • TraumaPony (unregistered) in reply to Synonymous Awkward
    Synonymous Awkward:
    TraumaPony:
    Stop beating a dead horse.

    I'd rather beat a TraumaPony.

    Rawr.
  • (cs) in reply to Aaron
    Aaron:
    Wow. I know that the comments here are usually pretty silly, but some of today's really are terrible. Let's recap .NET 101 here: ... 5. Enums in C# are far more flexible and easier to use than the retarded BitSet in Java. I'm not going to expand on that because it really ought to be obvious to anyone who's actually had experience with both languages (and if you haven't - keep quiet!) ...
    Yes. Enums are flexible in C#. Since they're integers. Let me present you with a similar construct in Java (or whatever language): "int". And here's how to use it: public class MyCppLikeEnum { public static final int FLAG1 = 0x01; public static final int FLAG2 = 0x02; public static final int FLAG3 = 0x04; } It's wonderful, really ;-) And then you have type safe enums for when you want to write maintainable code.
  • bystander (unregistered) in reply to Matt

    What happens when your database goes down?

  • bystander (unregistered) in reply to Matt
    Matt:
    WTF?

    Why are the valid values for the Enum hard-coded. They should be queried from a database and loaded into an array. Then, if your valid values change, you don't have to change your code.

    Amateur

    I can't tell if you're being sarcastic =P

Leave a comment on “When an Enum's an Enum”

Log In or post as a guest

Replying to comment #:

« Return to Article