4

I have a enum

 public enum TestType:int { Aphasia = 2, FocusedAphasia = 5 } 

with values set. I want to change the value of the enum 'FocusedAphasia' from 5 to 10. Can anyone help me in changing the value of enum at runtime

11
  • 1
    Replace FocusedAphasia = 5 with FocusedAphasia = 10 Commented Oct 19, 2011 at 10:49
  • You cannot do that with an enumeration, just make it a property of the class, if you want to change the value of it. If you want to change it from 5 to 10 then simply change it. Commented Oct 19, 2011 at 10:50
  • 2
    @Ankur, how do you do that at runtime? Commented Oct 19, 2011 at 10:50
  • An enum is probably the wrong tool for what you are trying to do. Use a Dictionary instead. Commented Oct 19, 2011 at 10:52
  • 1
    @Ankur, you can't reload an assembly, unless you reload the whole AppDomain. Commented Oct 19, 2011 at 10:58

4 Answers 4

12

You cannot change enums at runtime. I'm not sure why you'd ever need to, but in any case it's not possible. Using a variable would be the alternative.

Sign up to request clarification or add additional context in comments.

Comments

7

You can't do this, it is a strongly-typed value, if you like.

The elements of the enum are read-only and changing them at runtime is not possible, nor would it be desirable.

What might suit you is extension of the enum to expose a new value to use with new features and whatnot, such as:

public enum TestType:int { Aphasia = 2, FocusedAphasia = 5 SomeOtherAphasia = 10 } 

Not being quite clued up on exactly what you want to do, I can't well suggest much else.

Comments

3

Well, this question is 7 years far while I am writing my answer. I still want to write it, maybe it will be useful for someone later.

Changing enum values in runtime is not possible, but there is a way to play around by casting int variable into enum, and defining those ints with their values in a dictionary as in below:

// Define enum TestType without values enum TestType{} // Define a dictionary for enum values Dictionary<int,string> d = new Dictionary<int,string>(); void Main() { int i = 5; TestType s = (TestType)i; TestType e = (TestType)2; // Definging enum int values with string values d.Add(2,"Aphasia"); d.Add(5,"FocusedAphasia"); // Results: Console.WriteLine(d[(int)s]); // Result: FocusedAphasia Console.WriteLine(d[(int)e]); // Result: Aphasia } 

In this way, you have a dynamic dictionary for enum values without any written inside it. In case you want any other value for the enum, then you can make a method to add it:

public void NewEnumValue(int i, string v) { try { string test = d[i]; Console.WriteLine("This Key is already assigned with value: " + test); } catch { d.Add(i,v); } } 

So, your last code in use should be this way:

// Define enum TestType without values enum TestType{} // Define a dictionary for enum values Dictionary<int,string> d = new Dictionary<int,string>(); public void NewEnumValue(int i, string v) { try { string test = d[i]; Console.WriteLine("This Key is already assigned with value: " + test); } catch { d.Add(i,v); Console.WriteLine("Addition Done!"); } } void Main() { int i = 5; TestType s = (TestType)i; TestType e = (TestType)2; // Definging enum int values with string values NewEnumValue(2,"Aphasia"); NewEnumValue(5,"FocusedAphasia"); Console.WriteLine("You will add int with their values; type 0 to " + "exit"); while(true) { Console.WriteLine("enum int:"); int ii = Convert.ToInt32(Console.ReadLine()); if (ii == 0) break; Console.WriteLine("enum value:"); string v = Console.ReadLine(); Console.WriteLine("will try to assign the enum TestType with " + "value: " + v + " by '" + ii + "' int value."); NewEnumValue(ii,v); } // Results: Console.WriteLine(d[(int)s]); // Result: FocusedAphasia Console.WriteLine(d[(int)e]); // Result: Aphasia } 

Comments

2

Well actually you can. Say you have an assembly (dll) with the original TestType. You can unload that assembly (this is somewhat complicated), rewrite the assembly with a new TestType and reload it.

You can not, however, change the type of existing variables because those have to be disposed of before the assembly can be unloaded.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.