Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. Thank god the else part is not the same!

Thank god the else part is not the same!

Scheduled Pinned Locked Moved The Weird and The Wonderful
jsoncom
5 Posts 5 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    VallarasuS
    wrote on last edited by
    #1

    Fortunately there were three cases alone, other wise he would have ended up parsing the same enum multiple times! Comments are mine, rest remains the same!

            foreach (string name in names)
            {
                if (StringValueOf((Enum)Enum.Parse(enumType, name)).Equals(value))
                {
                    return Enum.Parse(enumType, name); // <------ 
                }
                else if (AlternateStringValueOf((Enum)Enum.Parse(enumType, name)).Equals(value))
                {
                    return Enum.Parse(enumType, name); // <------ 
                }
                else if (name.Equals(value))
                {
                    return Enum.Parse(enumType, name); // // <------ 
                }
            }
    

    Regards Vallarasu S | FSharpMe.blogspot.com

    B B B P 4 Replies Last reply
    0
    • V VallarasuS

      Fortunately there were three cases alone, other wise he would have ended up parsing the same enum multiple times! Comments are mine, rest remains the same!

              foreach (string name in names)
              {
                  if (StringValueOf((Enum)Enum.Parse(enumType, name)).Equals(value))
                  {
                      return Enum.Parse(enumType, name); // <------ 
                  }
                  else if (AlternateStringValueOf((Enum)Enum.Parse(enumType, name)).Equals(value))
                  {
                      return Enum.Parse(enumType, name); // <------ 
                  }
                  else if (name.Equals(value))
                  {
                      return Enum.Parse(enumType, name); // // <------ 
                  }
              }
      

      Regards Vallarasu S | FSharpMe.blogspot.com

      B Offline
      B Offline
      Brisingr Aerowing
      wrote on last edited by
      #2

      :doh:

      I think computer viruses should count as life. I think it says something about human nature that the only form of life we have created so far is purely destructive. We've created life in our own image. Stephen Hawking

      1 Reply Last reply
      0
      • V VallarasuS

        Fortunately there were three cases alone, other wise he would have ended up parsing the same enum multiple times! Comments are mine, rest remains the same!

                foreach (string name in names)
                {
                    if (StringValueOf((Enum)Enum.Parse(enumType, name)).Equals(value))
                    {
                        return Enum.Parse(enumType, name); // <------ 
                    }
                    else if (AlternateStringValueOf((Enum)Enum.Parse(enumType, name)).Equals(value))
                    {
                        return Enum.Parse(enumType, name); // <------ 
                    }
                    else if (name.Equals(value))
                    {
                        return Enum.Parse(enumType, name); // // <------ 
                    }
                }
        

        Regards Vallarasu S | FSharpMe.blogspot.com

        B Offline
        B Offline
        Bernhard Hiller
        wrote on last edited by
        #3

        He made his solution future-proof: somewhen in the future, Enum.Parse might return different results depending on the cases he described here. Be prepared!

        1 Reply Last reply
        0
        • V VallarasuS

          Fortunately there were three cases alone, other wise he would have ended up parsing the same enum multiple times! Comments are mine, rest remains the same!

                  foreach (string name in names)
                  {
                      if (StringValueOf((Enum)Enum.Parse(enumType, name)).Equals(value))
                      {
                          return Enum.Parse(enumType, name); // <------ 
                      }
                      else if (AlternateStringValueOf((Enum)Enum.Parse(enumType, name)).Equals(value))
                      {
                          return Enum.Parse(enumType, name); // <------ 
                      }
                      else if (name.Equals(value))
                      {
                          return Enum.Parse(enumType, name); // // <------ 
                      }
                  }
          

          Regards Vallarasu S | FSharpMe.blogspot.com

          B Offline
          B Offline
          BillW33
          wrote on last edited by
          #4

          That is rather sad code. :sigh:

          Just because the code works, it doesn't mean that it is good code.

          1 Reply Last reply
          0
          • V VallarasuS

            Fortunately there were three cases alone, other wise he would have ended up parsing the same enum multiple times! Comments are mine, rest remains the same!

                    foreach (string name in names)
                    {
                        if (StringValueOf((Enum)Enum.Parse(enumType, name)).Equals(value))
                        {
                            return Enum.Parse(enumType, name); // <------ 
                        }
                        else if (AlternateStringValueOf((Enum)Enum.Parse(enumType, name)).Equals(value))
                        {
                            return Enum.Parse(enumType, name); // <------ 
                        }
                        else if (name.Equals(value))
                        {
                            return Enum.Parse(enumType, name); // // <------ 
                        }
                    }
            

            Regards Vallarasu S | FSharpMe.blogspot.com

            P Offline
            P Offline
            Pete OHanlon
            wrote on last edited by
            #5

            Because sometimes it's not enough to evaluate it once. I suppose, for good measure, he could have put an Enum val = (Enum)Enum.Parse(enumType, name); immediately after the for.

            *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

            "Mind bleach! Send me mind bleach!" - Nagy Vilmos

            CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups