Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting enum value at runtime in C#

Tags:

c#

enums

runtime

Is there any way that I can change enum values at run-time?

e.g I have following type

enum MyType
{
   TypeOne, //=5 at runtime 
   TypeTwo  //=3 at runtime
}

I want at runtime set 5 to TypeOne and 3 to TypeTwo.

like image 425
Masoud Avatar asked Nov 09 '14 11:11

Masoud


1 Answers

As others have pointed out, the answer is no.

You could however probably refactor your code to use a class instead:

public sealed class MyType
{
   public int TypeOne { get; set; }
   public int TypeTwo { get; set; }
}

...

var myType = new MyType { TypeOne  = 5, TypeTwo = 3 };

or variations on that theme.

like image 154
Stephen Kennedy Avatar answered Oct 19 '22 05:10

Stephen Kennedy