• (cs)
    //FIRST - BRILLANT - PAULA - IRISH GIRL
    int _applicationId = Convert.ToInt32(ApplicationUserInfo.Current.ApplicationType.ToString());
    
  • larsy87 (unregistered) in reply to ParkinT

    thats 2 lines!

    captcha - 'nobis' almost noobis...sheeeesh.

  • tragomaskhalos (unregistered)

    It's a goddamned Greek stradegy

  • Anon (unregistered) in reply to ParkinT

    int _applicationId = Convert.ToInt32(ApplicationUserInfo.Current.ApplicationType);

    ?

  • (cs)

    int _applicationId = (int)ApplicationUserInfo.Current.ApplicationType;

  • Fool (unregistered)

    Never even looked at c# in my life so I have to ask.

    Couldn't you jsut type _applicationId as an enum?

    CAPTCHA: jumentum ... made me giggle for some reason.

  • (cs)

    Wouldn't this be enough?

    int _applicationId = ApplicationUserInfo.Current.ApplicationType;

    Also... I wonder what 0 is.... probably something very dark and mysterious.

  • - (unregistered)

    We have the EE version of this at my work, designed by The Architect Himself. It spans two maven modules but does roughly the same.

  • Burzmali (unregistered)

    All of your suggestions ignore the case where the value of the ApplicationType was in the Enum but not in the hard coded list. Not the best method for handling something like a sale, but it would be functional.

  • bebna (unregistered) in reply to Fool

    jumentum? Where was it...

    Ah, there: http://hardware.slashdot.org/story/11/10/23/0353245/jumentum-introduces-a-single-chip-linux-system

  • Anon (unregistered)

    int _applicationId = (int)ApplicationUserInfo.Current.ApplicationType;

  • Zach (unregistered)

    My first question is do you even need to store it in another variable. Why not just access ApplicationUserInfo.Current.ApplicationType when needed? Possibly they need it because ApplicationUserInfo is going to go out of scope. If you must store into another variable--

    ApplicationTypeEnum _app = ApplicationUserInfo.Current.ApplicationType;

  • Anon (unregistered) in reply to ParkinT

    LOL, nice wtf throwing an exception :-)

  • (cs)

    WTF 1: You need to modify this routine every time that ApplicationTypeEnum changes WTF 2: Converting an enum to a string and then referring to the enum again WTF 3: Having a getter method called 'Set...' WTF 4: No default case, but instead returning 0 at a different location

    Can be rewritten as

        return Convert.toInt32(ApplicationUserInfo.Current.ApplicationType);

    Note: I don't do C#, so if there's a problem with the libraries or the Convert.toInt32, I wouldn't know. In Java, I'd have written it as follows:

        return applicationUserInfo.current.applicationType.ordinal();
  • Bob Newbie (unregistered)
    int _applicationId = Convert.ToInt32(Enum.Parse(typeof(ApplicationTypeEnum), (from e in Enum.GetNames(typeof(ApplicationTypeEnum)) where e == ApplicationUserInfo.Current.ApplicationType.ToString() select e).First()));

    Right?

  • Zach (unregistered) in reply to Bob Newbie

    you're the winner!!

  • Topper (unregistered) in reply to Severity One
    Severity One:
    WTF 1: You need to modify this routine every time that ApplicationTypeEnum changes WTF 2: Converting an enum to a string and then referring to the enum again WTF 3: Having a getter method called 'Set...' WTF 4: No default case, but instead returning 0 at a different location

    Can be rewritten as

        return Convert.toInt32(ApplicationUserInfo.Current.ApplicationType);

    Note: I don't do C#, so if there's a problem with the libraries or the Convert.toInt32, I wouldn't know. In Java, I'd have written it as follows:

        return applicationUserInfo.current.applicationType.ordinal();

    WTF5: in C# you're supposed to use Enum.GetName(...) instead of ToString() (see the Remarks section of the related MSDN article). WTF6: Convert.ToInt32() is useless when you can just cast to int. WTF7: there was probably no reason to convert it in the beginning.

  • Black Bart (unregistered) in reply to Bob Newbie
    Bob Newbie:
    int _applicationId = Convert.ToInt32(Enum.Parse(typeof(ApplicationTypeEnum), (from e in Enum.GetNames(typeof(ApplicationTypeEnum)) where e == ApplicationUserInfo.Current.ApplicationType.ToString() select e).First()));

    Right?

    I like the original WTF better than this, even though this might be correct.

  • HP PhaserJet (unregistered)

    There's no need to harp your trumpet on this fiddle of a flute. Everywhere you will find microcosms of the bizarre is a world that appears uniformly adherent to the edicts of The Brotherhood, but only to the shallow-minded.

    Why can't we escape this torrent of confusion?

    I would love to. But I would also love to not. Therein lies the crux of our misery and listlessness.

    Free the surface of your home planet from your grasp and embrace the void. It will embrace you as well. It will love you more than any mammalian biped ever could - it will love you without end.

    Good luck on your journey, brothers and sisters in code!

  • Anon (unregistered) in reply to TheSHEEEP
    TheSHEEEP:
    Wouldn't this be enough?

    int _applicationId = ApplicationUserInfo.Current.ApplicationType;

    Also... I wonder what 0 is.... probably something very dark and mysterious.

    No. You need the explicit cast to int or the compiler will complain.

    int _applicationId = (int) ApplicationUserInfo.Current.ApplicationType;

  • Topper (unregistered) in reply to Topper
    Topper:
    Severity One:
    WTF 1: You need to modify this routine every time that ApplicationTypeEnum changes WTF 2: Converting an enum to a string and then referring to the enum again WTF 3: Having a getter method called 'Set...' WTF 4: No default case, but instead returning 0 at a different location

    Can be rewritten as

        return Convert.toInt32(ApplicationUserInfo.Current.ApplicationType);

    Note: I don't do C#, so if there's a problem with the libraries or the Convert.toInt32, I wouldn't know. In Java, I'd have written it as follows:

        return applicationUserInfo.current.applicationType.ordinal();

    WTF5: in C# you're supposed to use Enum.GetName(...) instead of ToString() (see the Remarks section of the related MSDN article). WTF6: Convert.ToInt32() is useless when you can just cast to int. WTF7: there was probably no reason to convert it in the beginning.

    And also WTF8: I don't know what happens when you do all these ToString() stuff if it's a [Flags] enum, but it must not be pretty.

  • Anon (unregistered) in reply to Zach
    Zach:
    My first question is do you even need to store it in another variable.

    The only reason I can think off is for convenience (save typing ApplicationUserInfo.Current.ApplicationType - although intellisense makes this less of a problem) or maybe readability (perhaps _applicationId is a more meaningful name in the context of where it's being used).

  • Charles (unregistered)

    TRWTF is that C# lets you put any int into any enum, so if the ApplicationUserInfo.Current.ApplicationType == 54321, this method will return 0, not 54321 like all your other examples do. I doubt, however, that the pseudo-default case ever shows up. PS. Is "stradegy" another word for "strategery"?

  • Topper (unregistered) in reply to Topper
    Topper:
    Topper:
    Severity One:
    WTF 1: You need to modify this routine every time that ApplicationTypeEnum changes WTF 2: Converting an enum to a string and then referring to the enum again WTF 3: Having a getter method called 'Set...' WTF 4: No default case, but instead returning 0 at a different location

    Can be rewritten as

        return Convert.toInt32(ApplicationUserInfo.Current.ApplicationType);

    Note: I don't do C#, so if there's a problem with the libraries or the Convert.toInt32, I wouldn't know. In Java, I'd have written it as follows:

        return applicationUserInfo.current.applicationType.ordinal();

    WTF5: in C# you're supposed to use Enum.GetName(...) instead of ToString() (see the Remarks section of the related MSDN article). WTF6: Convert.ToInt32() is useless when you can just cast to int. WTF7: there was probably no reason to convert it in the beginning.

    And also WTF8: I don't know what happens when you do all these ToString() stuff if it's a [Flags] enum, but it must not be pretty.

    Yay, another one of my memes is catching on!

  • Zach (unregistered) in reply to ParkinT

    2011-10-24 08:16 • by ParkinT

    ParkinT:
    //FIRST - BRILLANT - PAULA - IRISH GIRL
    int _applicationId = Convert.ToInt32(ApplicationUserInfo.Current.ApplicationType.ToString());
    

    TRWTF, how did first post 2 days before the post was posted?

  • Too ashamed to put my name here (unregistered) in reply to Burzmali
    Burzmali:
    All of your suggestions ignore the case where the value of the ApplicationType was in the Enum but not in the hard coded list. Not the best method for handling something like a sale, but it would be functional.

    If an application type isn't defined, then you're probably facing a bigger problem.

    That said, the wonderful ternary operator may be used to obfuscate the solution into a single line:

    return Enum.IsDefined(typeof(ApplicationTypeEnum), ApplicationUserInfo.Current.ApplicationType) ? (int)ApplicationUserInfo.Current.ApplicationType : 0;

    Captcha: odio - odious code, yes.

  • Patrick (unregistered) in reply to Topper
    Topper:
    Severity One:
    WTF 1: You need to modify this routine every time that ApplicationTypeEnum changes WTF 2: Converting an enum to a string and then referring to the enum again WTF 3: Having a getter method called 'Set...' WTF 4: No default case, but instead returning 0 at a different location

    Can be rewritten as

        return Convert.toInt32(ApplicationUserInfo.Current.ApplicationType);

    Note: I don't do C#, so if there's a problem with the libraries or the Convert.toInt32, I wouldn't know. In Java, I'd have written it as follows:

        return applicationUserInfo.current.applicationType.ordinal();

    WTF5: in C# you're supposed to use Enum.GetName(...) instead of ToString() (see the Remarks section of the related MSDN article). WTF6: Convert.ToInt32() is useless when you can just cast to int. WTF7: there was probably no reason to convert it in the beginning.

    WTF8: given the obvious BRILLIANT mind behind this code, 0 probably even refers to the first item in the enum.

  • C# Developer (unregistered)

    I suspect this at least partially a fictional WTF, that code would never compile without break; after each return;

  • Josh (unregistered) in reply to Anon
    Anon:
    Zach:
    My first question is do you even need to store it in another variable.

    The only reason I can think off is for convenience (save typing ApplicationUserInfo.Current.ApplicationType - although intellisense makes this less of a problem) or maybe readability (perhaps _applicationId is a more meaningful name in the context of where it's being used).

    If you're in a performance sensitive environment assigning to a local value type variable would be much faster then referencing a property of a static class repeatedly.

  • (cs)
    return (int)(from ApplicationTypeEnum a in Enum.GetValues(typeof[/blue]([color=#00AAAA]ApplicationTypeEnum)) where a.ToString() == (from string e in Enum.GetNames(typeof[/blue]([color=#00AAAA]ApplicationTypeEnum)).ToList() where Regex.IsMatch(e, ApplicationUserInfo.Current.ApplicationType.ToString()) select e) select a).First();

    Addendum (2011-10-26 10:00): Whoops, screwed the bbcode.

  • (cs) in reply to C# Developer
    C# Developer:
    I suspect this at least partially a fictional WTF, that code would never compile without break; after each return;
    I'm no C# Developer, but if I'm not mistaken "return" counts as a jump statement.
  • Mmmpf (unregistered) in reply to C# Developer
    C# Developer:
    I suspect this at least partially a fictional WTF, that code would never compile without break; after each return;

    I suspect you're at least partially a C# developer, that code would definitely compile with return, which is a jump statement, as are break, goto case, and throw.

  • Mmmpf (unregistered) in reply to boog
    boog:
    C# Developer:
    I suspect this at least partially a fictional WTF, that code would never compile without break; after each return;
    I'm no C# Developer, but if I'm not mistaken "return" counts as a jump statement.

    damn, burned!

  • Digger (unregistered)

    If Alex told you jump outta window, y'all do, right?

    Okay real fix now:

    1. Delete whole applicationId and ApplicationType crap
    2. File->New Project->Name: Strategy2 File->New Project->Name: AdDetector etc.
    3. Add reference to DLL containing base classes you need to inherit from 4...
    4. PROFIT! Bonus question: where Strategy1 went wrong
  • James Miller (unregistered) in reply to HP PhaserJet

    Dammit, the printer got onto the internet again.

  • (cs)
    return (int)(from ApplicationTypeEnum a in Enum.GetValues(typeof(ApplicationTypeEnum)) where a.ToString() == (from string e in Enum.GetNames(typeof(ApplicationTypeEnum)).ToList() where Regex.IsMatch(e, ApplicationUserInfo.Current.ApplicationType.ToString()) select e) select a).First();
  • (cs)

    The Real WTF: They spelled "Stragedy" wrong.

  • Mmmpf (unregistered) in reply to Digger
    Digger:
    If Alex told you jump outta window, y'all do, right?

    Okay real fix now:

    1. Delete whole applicationId and ApplicationType crap
    2. File->New Project->Name: Strategy2 File->New Project->Name: AdDetector etc.
    3. Add reference to DLL containing base classes you need to inherit from 4...
    4. PROFIT! Bonus question: where Strategy1 went wrong

    Doesn't matter if it went wrong, one always needs to have a backup stradegy.

  • Nagesh (unregistered) in reply to @Deprecated
    @Deprecated:
    The Real WTF: They spelled "Stragedy" wrong.
    s/S//
  • (cs) in reply to frits
    frits:
    return (int)(from ApplicationTypeEnum a in Enum.GetValues(typeof(ApplicationTypeEnum)) where a.ToString() == (from string e in Enum.GetNames(typeof(ApplicationTypeEnum)).ToList() where Regex.IsMatch(e, ApplicationUserInfo.Current.ApplicationType.ToString()) select e) select a).First();
    How you managed to shoe-horn regex with parsing enums is just beyond me. Congrats...

    Also, based on use of color tags, I'm guessing it's a slow morning? :)

    Technical side note that I'm sure you know: the Enum.GetNames isn't guaranteed to return the correct (expected) enum name, especially when you obfuscate the assembly or build in release. One should use the DescriptionAttribute to decorate your enums.

  • Bronie (unregistered)
    int _applicationId = 42; //arbitrary unique number 
  • That guy over there (unregistered) in reply to Too ashamed to put my name here
    Too ashamed to put my name here:
    return Enum.IsDefined(typeof(ApplicationTypeEnum), ApplicationUserInfo.Current.ApplicationType) ? (int)ApplicationUserInfo.Current.ApplicationType : 0;
    While your solution takes care of people assigning arbitary integers to ApplicationType, the method was obviously created to disallow Stradegy1, so it needs a little fix.
    return Enum.IsDefined(typeof(ApplicationTypeEnum), ApplicationUserInfo.Current.ApplicationType) && ApplicationUserInfo.Current.ApplicationType.ToString() != ApplicationTypeEnum.Stradegy1.ToString() ? (int)ApplicationUserInfo.Current.ApplicationType : 0;
  • airdrik (unregistered) in reply to Digger
    Digger:
    If Alex told you jump outta window, y'all do, right?

    Okay real fix now:

    1. Delete whole applicationId and ApplicationType crap
    2. File->New Project->Name: Strategy2 File->New Project->Name: AdDetector etc.
    3. Add reference to DLL containing base classes you need to inherit from 4...
    4. PROFIT! Bonus question: where Strategy1 went wrong
    Stradegy1 was the first value of the enum (with integer value 0), and since it is defaulting to 0 if the value wasn't found in the expected list, he figured he could just skip checking for it because it would return 0 anyway. That's opdimization there!
  • airdrik (unregistered) in reply to C# Developer
    C# Developer:
    I suspect this at least partially a fictional WTF, that code would never compile without break; after each return;
    Well trolled! you got two bytes!
  • (cs) in reply to Bronie
    Bronie:
    int _applicationId = 42; //arbitrary unique number 

    A bit like the old joke:

    int function getRandomInt() {
        return 3; // I rolled it on a die so it's as random as you'll get
    }
    
  • Anonymous (unregistered)

    You mean your curiosity SET the better of you...

    return Convert.ToInt32(Enum.Parse(typeof(ApplicationTypeEnum), ApplicationUserInfo.Current.ApplicationType.ToString()));

    In other words: return OCR(TakePicture(Print(ApplicationUserInfo.Current.ApplicationType.ToString())));

    Parentheses pried from McCarthy's cold dead hands (RIP).

  • (cs)

    Another question to ask is where and how is

    ApplicationUserInfo.Current.ApplicationType
    set?

    It's just a guess, but it seems that ApplicationUserInfo.Current.ApplicationType is probably only ever set once, most likely on app startup, and likely not based on user input. It should be validated only once before setting ApplicationUserInfo.Current.ApplicationType, and if it's not defined, throw the exception there.

    That way you never have to validate that value again.

  • (cs) in reply to C-Octothorpe
    C-Octothorpe:
    based on use of color tags, I'm guessing it's a slow morning?
    Ever since Hortical's disproportionate reaction to my use of color tags, I'm trying to use them whenever possible, slow morning or not.
  • (cs) in reply to frits
    frits:
    C-Octothorpe:
    based on use of color tags, I'm guessing it's a slow morning?
    Ever since Hortical's disproportionate reaction to my use of color tags, I'm trying to use them whenever possible, slow morning or not.
    I always knew you were a bastard... :)
  • (cs) in reply to airdrik
    airdrik:
    C# Developer:
    I suspect this at least partially a fictional WTF, that code would never compile without break; after each return;
    Well trolled! you got two bytes!
    Right, because erroneous comments are unheard of on this site. I keep forgetting how everyone who posts here is a genius.

Leave a comment on “Count The WTF: Conversion Go-Round”

Log In or post as a guest

Replying to comment #:

« Return to Article