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