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. General Programming
  3. C#
  4. enum question

enum question

Scheduled Pinned Locked Moved C#
tutorialquestion
4 Posts 3 Posters 0 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.
  • A Offline
    A Offline
    akkram
    wrote on last edited by
    #1

    Hi, is there a way to use an enum as condition in an if statement. Say for example can I change the following code and use enum instead /thx: string houseType = string.Empty; //is set to different housetypes ... ... if(housType.Equals("detached house")) ..do something else if(houseType.Equals("manor")) ..do something else

    L 1 Reply Last reply
    0
    • A akkram

      Hi, is there a way to use an enum as condition in an if statement. Say for example can I change the following code and use enum instead /thx: string houseType = string.Empty; //is set to different housetypes ... ... if(housType.Equals("detached house")) ..do something else if(houseType.Equals("manor")) ..do something else

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Sure:

      enum HouseType {UNKNOWN, DETACHED, ... }

      HouseType myHouse=HouseType.UNKNOWN;
      ...
      if (myHouse==HouseType.DETACHED) ...
      ...

      Now if you want enum values that are not valid identifiers, you need some special trick, e.g. add Attributes to each enum value, and retrieve them using reflection. :)

      Luc Pattyn [My Articles] [Forum Guidelines]

      A D 2 Replies Last reply
      0
      • L Luc Pattyn

        Sure:

        enum HouseType {UNKNOWN, DETACHED, ... }

        HouseType myHouse=HouseType.UNKNOWN;
        ...
        if (myHouse==HouseType.DETACHED) ...
        ...

        Now if you want enum values that are not valid identifiers, you need some special trick, e.g. add Attributes to each enum value, and retrieve them using reflection. :)

        Luc Pattyn [My Articles] [Forum Guidelines]

        A Offline
        A Offline
        akkram
        wrote on last edited by
        #3

        Many thx Luc :)

        1 Reply Last reply
        0
        • L Luc Pattyn

          Sure:

          enum HouseType {UNKNOWN, DETACHED, ... }

          HouseType myHouse=HouseType.UNKNOWN;
          ...
          if (myHouse==HouseType.DETACHED) ...
          ...

          Now if you want enum values that are not valid identifiers, you need some special trick, e.g. add Attributes to each enum value, and retrieve them using reflection. :)

          Luc Pattyn [My Articles] [Forum Guidelines]

          D Offline
          D Offline
          DavidNohejl
          wrote on last edited by
          #4

          Or use static class, like

          class HouseType
          {
          HouseType(string){...}

            string desc;
            public string Description { get {return desc;}}
          

          }

          static class HouseTypes
          {
          static HouseTypes()
          {
          type1 = new HouseType("House of type1");
          }
          public HouseType Type1 { get {return type1}}
          }

          ...

          if(myHouse.HouseType == HouseTypes.Type1)...

          It should have better performance then using attributes but it consumes some extra memory.


          "Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe

          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