-2

I have the following code in C#

 class Program
    {
        static void Main(string[] args)
        {

            string text;

            Console.WriteLine(fooClass.RunInfoRequestType.text2);

            Console.Out.WriteLine("\nPress [Enter] key to close ...");
            Console.ReadLine();

        }

        public static class fooClass
        {
            public enum RunInfoRequestType
            {

                text1 = 1,
                text2 = 2,
                text3 = 3,
                text4 = 4,
                text5 = 5,
                text6 = 6,
                text7 = 7,
                text8 = 8
            }

        }


    }

I would like to know how could I pass dynamically the string text value to the fooClass.RunInfoRequestType.text2.

For instance, set "text3" to the string text and pass the string to the fooClass.RunInfoRequestType.[text]. I don't want to change the enum at runtime. Just, pass the text string as a parameter to get the enum value.

JosepB
  • 2,205
  • 4
  • 20
  • 40
  • How about to use `Dictionary` and save text to `Value`? – Aleks Andreev Feb 14 '18 at 11:25
  • In c#, an enumeration type is a set of constants, so no, you can't change it dynamically. – Zohar Peled Feb 14 '18 at 11:27
  • NB: Not sure this is a duplicate... Is the question "How do I resolve a string to the corresponding enum entry?", or "How to I update the string value of an Enum entry at runtime?". The linked duplicate covers the first of these questions; but does not address the second. I'd interpreted the question as the second. – JohnLBevan Feb 14 '18 at 11:41
  • In fact this question is unclear at best. I, for one, don't know if the OP is asking how to add values to the existing enum at runtime (that's impossible, as my first comment and Liquid Core's answer states), or how to parse a string to the name of an enum member, or how to rename an enum member at runtime (also impossible, of course). – Zohar Peled Feb 14 '18 at 11:47

3 Answers3

1

If I got it correctly, you need to use string text value to get the corresponding enum item:

RunInfoRequestType item = (RunInfoRequestType)System.Enum.Parse(typeof(RunInfoRequestType), text);

You can put this code in a method:

public static GetRunInfoRequestType(string src) {
return (RunInfoRequestType)System.Enum.Parse(typeof(RunInfoRequestType), text);
}

This code is without checking the value of src if it's correct or not.

Dabbas
  • 3,112
  • 7
  • 42
  • 75
1

Enums are compile time constants. You cannot change them at run time.

Liquid Core
  • 1
  • 6
  • 27
  • 52
0

Is this what you're after; i.e. a way to have a run time display value for each enum value?

//initialise a dictionary to map your enum values to their runtime labels
IDictionary<RunInfoRequestType,string> labels = new Dictionary<RunInfoRequestType,string>
{
    { RunInfoRequestType.text1, RunInfoRequestType.text1.ToString() }
    ,{ RunInfoRequestType.text2, RunInfoRequestType.text2.ToString() }
    ,{ RunInfoRequestType.text3, RunInfoRequestType.text3.ToString() }
    ,{ RunInfoRequestType.text4, RunInfoRequestType.text4.ToString() }
    ,{ RunInfoRequestType.text5, RunInfoRequestType.text5.ToString() }
    ,{ RunInfoRequestType.text6, RunInfoRequestType.text6.ToString() }
    ,{ RunInfoRequestType.text7, RunInfoRequestType.text7.ToString() }
    ,{ RunInfoRequestType.text8, RunInfoRequestType.text8.ToString() }
};


//set text3 to string text
labels[RunInfoRequestType.text3] = text;

If so, the above will do it, but you may want to opt for a non-enum solution; e.g.

public class RunInfoRequestType 
{
    public int Id {get; private set;}
    public string DisplayName {get; set;}
    protected static readonly IDictionary<int,RunInfoRequestType> values = new Dictionary<int,RunInfoRequestType>();
    protected RunInfoRequestType (int id, string displayName)
    {
        this.Id = id;
        this.DisplayName = displayName;
        values.Add(id, this);
    }
    public static readonly RunInfoRequestType Text1 = new RunInfoRequestType(1, "text1");
    public static readonly RunInfoRequestType Text2 = new RunInfoRequestType(2, "text2");
    public static readonly RunInfoRequestType Text3 = new RunInfoRequestType(3, "text3");
    public static readonly RunInfoRequestType Text4 = new RunInfoRequestType(4, "text4");
    public static readonly RunInfoRequestType Text5 = new RunInfoRequestType(5, "text5");
    public static readonly RunInfoRequestType Text6 = new RunInfoRequestType(6, "text6");
    public static readonly RunInfoRequestType Text7 = new RunInfoRequestType(7, "text7");
    public static readonly RunInfoRequestType Text8 = new RunInfoRequestType(8, "text8");

    public static implicit operator int(RunInfoRequestType runInfoRequestType)
    {
        return runInfoRequestType.Id;
    }

    public static implicit operator RunInfoRequestType(int id)
    {
        RunInfoRequestType runInfoRequestType ;
        values.TryGetValue(id, out runInfoRequestType);
        return runInfoRequestType ;
    }

    public override string ToString()
    {
        return this.DisplayName;
    }
}

//set text3's display name to string text
RunInfoRequestType.Text3.DisplayName = text;
JohnLBevan
  • 22,735
  • 13
  • 96
  • 178