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. dropdown to enum

dropdown to enum

Scheduled Pinned Locked Moved C#
question
11 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.
  • F fififlowertot

    Hi, I want to cast the string output of the selected value from dropdown to an enum. What is the best way to do it? Thanks,

    OriginalGriffO Offline
    OriginalGriffO Offline
    OriginalGriff
    wrote on last edited by
    #2
    Enum.Parse(typeof(enumName), stringNameOfEnumValue);
    

    or (.NET 4.0 and above only)

    Enum.TryParse(stringNameOfEnumValue, out placeToPutValue);
    

    Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."

    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
    "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

    F B 2 Replies Last reply
    0
    • OriginalGriffO OriginalGriff
      Enum.Parse(typeof(enumName), stringNameOfEnumValue);
      

      or (.NET 4.0 and above only)

      Enum.TryParse(stringNameOfEnumValue, out placeToPutValue);
      

      Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."

      F Offline
      F Offline
      fififlowertot
      wrote on last edited by
      #3

      I tried, Enum.Parse(((System.Type)n1.n2.n3.enum1), stringval,true); but gives error that enum1 is a type not valid in current context where n1.n2.n3 is the namespace where public enum enum1 is declared.

      M OriginalGriffO 2 Replies Last reply
      0
      • OriginalGriffO OriginalGriff
        Enum.Parse(typeof(enumName), stringNameOfEnumValue);
        

        or (.NET 4.0 and above only)

        Enum.TryParse(stringNameOfEnumValue, out placeToPutValue);
        

        Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."

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

        Good answer, but just thought I'd mention that you then have to cast the result back:

        MyEnum value = (MyEnum)Enum.Parse(typeof(MyEnum), str);

        Also, it will throw an exception (FormatException, I think?) if the value is invalid so if this is being run from user input where that is a possibility, it should be caught.

        OriginalGriffO F 2 Replies Last reply
        0
        • F fififlowertot

          I tried, Enum.Parse(((System.Type)n1.n2.n3.enum1), stringval,true); but gives error that enum1 is a type not valid in current context where n1.n2.n3 is the namespace where public enum enum1 is declared.

          M Offline
          M Offline
          musefan
          wrote on last edited by
          #5

          Then try what was actually suggested instead

          I may or may not be responsible for my own actions

          1 Reply Last reply
          0
          • F fififlowertot

            I tried, Enum.Parse(((System.Type)n1.n2.n3.enum1), stringval,true); but gives error that enum1 is a type not valid in current context where n1.n2.n3 is the namespace where public enum enum1 is declared.

            OriginalGriffO Offline
            OriginalGriffO Offline
            OriginalGriff
            wrote on last edited by
            #6

            As musefan said, try what I gave you, not an interpretation of it: For example:

                public enum DriveType : int
                    {
                    Unknown = 0,
                    NoRoot = 1,
                    Removable = 2,
                    Localdisk = 3,
                    Network = 4,
                    CD = 5,
                    RAMDrive = 6
                    }
            
                ...
            
                DriveType dt = (DriveType) Enum.Parse(typeof(DriveType), "RAMDrive");
            

            Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."

            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
            "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

            1 Reply Last reply
            0
            • B BobJanova

              Good answer, but just thought I'd mention that you then have to cast the result back:

              MyEnum value = (MyEnum)Enum.Parse(typeof(MyEnum), str);

              Also, it will throw an exception (FormatException, I think?) if the value is invalid so if this is being run from user input where that is a possibility, it should be caught.

              OriginalGriffO Offline
              OriginalGriffO Offline
              OriginalGriff
              wrote on last edited by
              #7

              Those are two of the reasons I prefer the TryParse version. Unfortunately, it is only available in .NET 4.0 and above.

              Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
              "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

              1 Reply Last reply
              0
              • B BobJanova

                Good answer, but just thought I'd mention that you then have to cast the result back:

                MyEnum value = (MyEnum)Enum.Parse(typeof(MyEnum), str);

                Also, it will throw an exception (FormatException, I think?) if the value is invalid so if this is being run from user input where that is a possibility, it should be caught.

                F Offline
                F Offline
                fififlowertot
                wrote on last edited by
                #8

                hI, This worked for me: n1.n2.n3.enum = (n1.n2.n3.enum)Enum.Parse(typeof(n4.n5.n6.enum),stringval) where n1..6 are name spaces where respective enums are defined. Thanks for evryone's help.

                OriginalGriffO 1 Reply Last reply
                0
                • F fififlowertot

                  Hi, I want to cast the string output of the selected value from dropdown to an enum. What is the best way to do it? Thanks,

                  A Offline
                  A Offline
                  Alan N
                  wrote on last edited by
                  #9

                  If the item list of the combobox is fixed and contains valid enum values only, then it is ok to do a direct cast of the SelectedItem.

                  enum Fruit { Quince, Gooseberry, Damson }

                  public void InitialiseControls() {
                  FruitComboBox.DataSource = Enum.GetValues(typeof(Fruit));
                  }

                  private void EatFruitBtn_Click(object sender, EventArgs e) {
                  Fruit yummy = (Fruit)FruitComboBox.SelectedItem;
                  }

                  Alan.

                  1 Reply Last reply
                  0
                  • F fififlowertot

                    hI, This worked for me: n1.n2.n3.enum = (n1.n2.n3.enum)Enum.Parse(typeof(n4.n5.n6.enum),stringval) where n1..6 are name spaces where respective enums are defined. Thanks for evryone's help.

                    OriginalGriffO Offline
                    OriginalGriffO Offline
                    OriginalGriff
                    wrote on last edited by
                    #10

                    You definitely shouldn't do that! If you have any differences between the two enums in the different namespaces, you will get bad results. The only reason it doesn't complain is that Enum.Parse returns an object, which can be cast to either type. This is another reason why I prefer the TryParse method: It is strongly typed!

                    Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."

                    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                    "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                    B 1 Reply Last reply
                    0
                    • OriginalGriffO OriginalGriff

                      You definitely shouldn't do that! If you have any differences between the two enums in the different namespaces, you will get bad results. The only reason it doesn't complain is that Enum.Parse returns an object, which can be cast to either type. This is another reason why I prefer the TryParse method: It is strongly typed!

                      Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."

                      B Offline
                      B Offline
                      BobJanova
                      wrote on last edited by
                      #11

                      It's actually simple enough to create one and put it in a utility class (or even an extension method, if you are in 3.5).

                      T TryParseEnum(string s, T defaultValue){
                      try { return (T)Enum.Parse(typeof(T), s); }
                      catch(Exception) { return defaultValue; }
                      }

                      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