Click to See Complete Forum and Search --> : Converting a string to enum value ?[Resolved]
Pirate
Dec 30th, 2003, 01:21 AM
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 .
dynamic_sysop
Dec 30th, 2003, 06:31 AM
i knocked this up for you Pirate mate ...
enum en
{
value1=1,
value2=2
}
private void button8_Click(object sender, System.EventArgs e)
{
en x=new en();
object i=x.GetType().InvokeMember("value1",System.Reflection.BindingFlags.GetField,null,x,null);
int s=(int)i;
MessageBox.Show(s.ToString());
}
it should get you started. :)
Pirate
Dec 30th, 2003, 07:30 AM
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.....:D
Pirate
Dec 30th, 2003, 07:37 AM
Here's something similar to what I'm doing so far .
dynamic_sysop
Dec 30th, 2003, 08:07 AM
private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
DifferentNS.MyClass1.MyEnum x=new DifferentNS.MyClass1.MyEnum();
object i=x.GetType().InvokeMember(listBox1.SelectedItem.ToString(),System.Reflection.BindingFlags.GetField, null,x,null);
int s=(int)i;
label2.Text=s.ToString();
}
Pirate
Dec 30th, 2003, 08:14 AM
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 ? :p
dynamic_sysop
Dec 30th, 2003, 10:10 AM
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.
Pirate
Dec 30th, 2003, 01:26 PM
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 . :)
pvb
Dec 30th, 2003, 09:04 PM
here's a little different way of doin the same thing. Assume the following:
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:
private void listBox1_SelectedIndexChanged(object sender, System.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);
}
}
Pirate
Dec 30th, 2003, 09:24 PM
You're the man .
Thanks :)
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.