Results 1 to 10 of 10

Thread: Converting a string to enum value ?[Resolved]

  1. #1

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083

    Converting a string to enum value ?[Resolved]

    I populated a combobox with enum values , I created getter/setter to retrive/set these values .I can get the enum value as string but can't convert it to enum value to set the setter part .How to convert a string to the enum value. Thanks

    edit : without having to create string properties instead of EnumType propertry .
    Last edited by Pirate; Dec 30th, 2003 at 10:24 PM.

  2. #2
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    i knocked this up for you Pirate mate ...
    VB Code:
    1. [Color=Blue]enum[/color] en
    2.         {
    3.              value1=1,
    4.              value2=2
    5.         }
    6.  
    7.         [Color=Blue]private[/color] [Color=Blue]void[/color] button8_Click([Color=Blue]object[/color] sender, System.EventArgs e)
    8.         {
    9.              en x=[Color=Blue]new[/color] en();
    10.              [Color=Blue]object[/color] i=x.GetType().InvokeMember("value1",System.Reflection.BindingFlags.GetField,[Color=Blue]null[/color],x,[Color=Blue]null[/color]);
    11.             [Color=Blue]int[/color] s=([Color=Blue]int[/color])i;
    12.              MessageBox.Show(s.ToString());
    13.         }
    it should get you started.
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  3. #3

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    It converts from string to int , doesn't it ? But I need to convert the string to it's string corresponding enum value .

    Thanks man for the InvokeMember thingy.....
    Last edited by Pirate; Dec 30th, 2003 at 08:35 AM.

  4. #4

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Here's something similar to what I'm doing so far .
    Attached Files Attached Files

  5. #5
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    VB Code:
    1. private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
    2. {
    3.     DifferentNS.MyClass1.MyEnum x=new DifferentNS.MyClass1.MyEnum();
    4.     object i=x.GetType().InvokeMember(listBox1.SelectedItem.ToString(),System.Reflection.BindingFlags.GetField,null,x,null);
    5.     int s=(int)i;
    6.     label2.Text=s.ToString();
    7. }
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  6. #6

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Umm , not really what I'm after . Basically , I want to set the value of the EnumProperty (in the zip file above) from the listbox which is string type . I've tried many ways with casting but can't get it to work . There must be a way without having to change the property type . Any other suggestions ?

  7. #7
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    would you not be better building a struct? my understanding is that enums are set at design time. a struct you can do a lot more with.
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  8. #8

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Yeah , that's true , but I'm doing it the other way . I group some keywords that I'll use in my app then have a property that is declared as that enum (which can be set/get) . And use public property to get/set that obj . I can't change this implementation because it's based on other things . I solved it but with a little bit lengthy code though I'd loved to shorten that . Anyways ,I appreciate the help .

  9. #9
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    here's a little different way of doin the same thing. Assume the following:
    PHP Code:
    using System;

    namespace 
    DifferentNS
    {
        public class 
    MyClass1
        
    {
            private 
    MyEnum _myEnumProperty MyEnum.A;
            public 
    enum MyEnum
            
    {
                
    A,B,C,D,E
            
    }
            public 
    MyEnum MyEnumProperty
            
    {
                
    get
                
    {
                    return 
    _myEnumProperty;
                }
                
    set
                
    {
                    
    _myEnumProperty value;
                }
            }
        }

    you can do this:
    PHP Code:
    private void listBox1_SelectedIndexChanged(object senderSystem.EventArgs e)
    {
        
    setMyproperty.MyEnumProperty 
            (
    MyClass1.MyEnum)Enum.Parse(typeof(MyClass1.MyEnum), this.listBox1.Text);
        
    label2.Text =  setMyproperty.MyEnumProperty.ToString();
    }
    private 
    void EnumIteration()
    {
        foreach (
    string s in Enum.GetNames(typeof(MyClass1.MyEnum)))
        {
            
    this.listBox1.Items.Add(s);
        }            


  10. #10

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    You're the man .

    Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width