How to set a string in Enum C #?

I want to create an ENUM string in C #.

Basically I do not want to specify the form name in ENUM . When I open the form on the main page, I want to switch the case of the form name and open this particular form. I know that ENUM only accepts an integer, but I want to set it to string . Any idea?

+4
source share
7 answers

Do it:

 private IddFilterCompareToCurrent myEnum = (IddFilterCompareToCurrent )Enum.Parse(typeof(IddFilterCompareToCurrent[1]),domainUpDown1.SelectedItem.ToString()); 

[Enum.parse] returns an object, so you need to drop it.

+2
source

Enum may not be a string, but you can attach an attribute, and you can read the value of the enumeration, as shown below.

 public enum States { [Description("New Mexico")] NewMexico, [Description("New York")] NewYork, [Description("South Carolina")] SouthCarolina } public static string GetEnumDescription(Enum value) { FieldInfo fi = value.GetType().GetField(value.ToString()); DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes( typeof(DescriptionAttribute), false); if (attributes != null && attributes.Length > 0) return attributes[0].Description; else return value.ToString(); } 

here is a good article if you want to go through it: Linking strings with enums in C #

+18
source

As mentioned above, enumerations cannot be strings (or anything else but integers) in C #. I assume you came from Java? It would be nice if .NET had this function, where enums can be of any type.

As I usually get around this, use a static class:

 public static class MyValues { public static string ValueA { get { return "A"; } } public static string ValueB { get { return "B"; } } } 

Using this technique you can also use any type. You can call it the same way you used enumerations:

 if (value == MyValues.ValueA) { // do something } 
+5
source

I'm not sure if you understood you differently, but I think you're looking for this?

  public enum State { State1, State2, State3 }; public static State CurrentState = State.State1; if(CurrentState == State.State1) { //do something } 
+2
source

I don't think enumerations are the best solution for your problem. As noted by others, enumeration values ​​can only be integer values.

You can simply use Dictionary to store forms along with their name, for example:

 Dictionary<string, Form> formDict = new Dictionary<string, Form>(); private void addFormToDict(Form form) { formDict[form.Name] = form; } // ... addFormToDict(new MyFirstForm()); addFormToDict(new MySecondForm()); // ... add all forms you want to display to the dictionary if (formDict.ContainsKey(formName)) formDict[formName].Show(); else MessageBox.Show(String.Format("Couldn't find form '{0}'", formName)); 
+2
source

Or make the Enum member names exactly the way you want and use .ToString() ,

Write such a function ...

 string MyEnumString(MyEnum value) { const string MyEnumValue1String = "any string I like 1"; const string MyEnumValue2String = "any string I like 2"; ... switch (value) { case MyEnum.Value1: return MyEnumValue1String; case MyEnum.Value2: return MyEnumValue2String; ... } } 

Or use some dictionary or hash set of values ​​and strings.

+2
source

string enums do not exist in C #. See this related question .

Why don't you use int (the default type for enums) instead of string ?

+1
source

Source: https://habr.com/ru/post/1394569/


All Articles