C# How to convert Enum Value to Int [duplicate] - c#

I have a class called Questions (plural). In this class there is an enum called Question (singular) which looks like this.
public enum Question
{
Role = 2,
ProjectFunding = 3,
TotalEmployee = 4,
NumberOfServers = 5,
TopBusinessConcern = 6
}
In the Questions class I have a get(int foo) function that returns a Questions object for that foo. Is there an easy way to get the integer value off the enum so I can do something like this Questions.Get(Question.Role)?

Just cast the enum, e.g.
int something = (int) Question.Role;
The above will work for the vast majority of enums you see in the wild, as the default underlying type for an enum is int.
However, as cecilphillip points out, enums can have different underlying types.
If an enum is declared as a uint, long, or ulong, it should be cast to the type of the enum; e.g. for
enum StarsInMilkyWay:long {Sun = 1, V645Centauri = 2 .. Wolf424B = 2147483649};
you should use
long something = (long)StarsInMilkyWay.Wolf424B;

Since Enums can be any integral type (byte, int, short, etc.), a more robust way to get the underlying integral value of the enum would be to make use of the GetTypeCode method in conjunction with the Convert class:
enum Sides {
Left, Right, Top, Bottom
}
Sides side = Sides.Bottom;
object val = Convert.ChangeType(side, side.GetTypeCode());
Console.WriteLine(val);
This should work regardless of the underlying integral type.

Declare it as a static class having public constants:
public static class Question
{
public const int Role = 2;
public const int ProjectFunding = 3;
public const int TotalEmployee = 4;
public const int NumberOfServers = 5;
public const int TopBusinessConcern = 6;
}
And then you can reference it as Question.Role, and it always evaluates to an int or whatever you define it as.

On a related note, if you want to get the int value from System.Enum, then given e here:
Enum e = Question.Role;
You can use:
int i = Convert.ToInt32(e);
int i = (int)(object)e;
int i = (int)Enum.Parse(e.GetType(), e.ToString());
int i = (int)Enum.ToObject(e.GetType(), e);
The last two are plain ugly. I prefer the first one.

Question question = Question.Role;
int value = (int) question;
Will result in value == 2.

Example:
public enum EmpNo
{
Raj = 1,
Rahul,
Priyanka
}
And in the code behind to get the enum value:
int setempNo = (int)EmpNo.Raj; // This will give setempNo = 1
or
int setempNo = (int)EmpNo.Rahul; // This will give setempNo = 2
Enums will increment by 1, and you can set the start value. If you don't set the start value it will be assigned as 0 initially.

It's easier than you think - an enum is already an int. It just needs to be reminded:
int y = (int)Question.Role;
Console.WriteLine(y); // Prints 2

I have recently converted away from using enums in my code in favour of instead using classes with protected constructors and predefined static instances (thanks to Roelof - C# Ensure Valid Enum Values - Futureproof Method).
In light of that, below's how I'd now approach this issue (including implicit conversion to/from int).
public class Question
{
// Attributes
protected int index;
protected string name;
// Go with a dictionary to enforce unique index
//protected static readonly ICollection<Question> values = new Collection<Question>();
protected static readonly IDictionary<int,Question> values = new Dictionary<int,Question>();
// Define the "enum" values
public static readonly Question Role = new Question(2,"Role");
public static readonly Question ProjectFunding = new Question(3, "Project Funding");
public static readonly Question TotalEmployee = new Question(4, "Total Employee");
public static readonly Question NumberOfServers = new Question(5, "Number of Servers");
public static readonly Question TopBusinessConcern = new Question(6, "Top Business Concern");
// Constructors
protected Question(int index, string name)
{
this.index = index;
this.name = name;
values.Add(index, this);
}
// Easy int conversion
public static implicit operator int(Question question) =>
question.index; //nb: if question is null this will return a null pointer exception
public static implicit operator Question(int index) =>
values.TryGetValue(index, out var question) ? question : null;
// Easy string conversion (also update ToString for the same effect)
public override string ToString() =>
this.name;
public static implicit operator string(Question question) =>
question?.ToString();
public static implicit operator Question(string name) =>
name == null ? null : values.Values.FirstOrDefault(item => name.Equals(item.name, StringComparison.CurrentCultureIgnoreCase));
// If you specifically want a Get(int x) function (though not required given the implicit converstion)
public Question Get(int foo) =>
foo; //(implicit conversion will take care of the conversion for you)
}
The advantage of this approach is you get everything you would have from the enum, but your code's now much more flexible, so should you need to perform different actions based on the value of Question, you can put logic into Question itself (i.e. in the preferred OO fashion) as opposed to putting lots of case statements throughout your code to tackle each scenario.
NB: Answer updated 2018-04-27 to make use of C# 6 features; i.e. declaration expressions and lambda expression body definitions. See revision history for original code. This has the benefit of making the definition a little less verbose; which had been one of the main complaints about this answer's approach.

If you want to get an integer for the enum value that is stored in a variable, for which the type would be Question, to use for example in a method, you can simply do this I wrote in this example:
enum Talen
{
Engels = 1, Italiaans = 2, Portugees = 3, Nederlands = 4, Duits = 5, Dens = 6
}
Talen Geselecteerd;
public void Form1()
{
InitializeComponent()
Geselecteerd = Talen.Nederlands;
}
// You can use the Enum type as a parameter, so any enumeration from any enumerator can be used as parameter
void VeranderenTitel(Enum e)
{
this.Text = Convert.ToInt32(e).ToString();
}
This will change the window title to 4, because the variable Geselecteerd is Talen.Nederlands. If I change it to Talen.Portugees and call the method again, the text will change to 3.

One more way to do it:
Console.WriteLine("Name: {0}, Value: {0:D}", Question.Role);
It will result in:
Name: Role, Value: 2

To ensure an enum value exists and then parse it, you can also do the following.
// Fake Day of Week
string strDOWFake = "SuperDay";
// Real Day of Week
string strDOWReal = "Friday";
// Will hold which ever is the real DOW.
DayOfWeek enmDOW;
// See if fake DOW is defined in the DayOfWeek enumeration.
if (Enum.IsDefined(typeof(DayOfWeek), strDOWFake))
{
// This will never be reached since "SuperDay"
// doesn't exist in the DayOfWeek enumeration.
enmDOW = (DayOfWeek)Enum.Parse(typeof(DayOfWeek), strDOWFake);
}
// See if real DOW is defined in the DayOfWeek enumeration.
else if (Enum.IsDefined(typeof(DayOfWeek), strDOWReal))
{
// This will parse the string into it's corresponding DOW enum object.
enmDOW = (DayOfWeek)Enum.Parse(typeof(DayOfWeek), strDOWReal);
}
// Can now use the DOW enum object.
Console.Write("Today is " + enmDOW.ToString() + ".");

Use an extension method instead:
public static class ExtensionMethods
{
public static int IntValue(this Enum argEnum)
{
return Convert.ToInt32(argEnum);
}
}
And the usage is slightly prettier:
var intValue = Question.Role.IntValue();

public enum QuestionType
{
Role = 2,
ProjectFunding = 3,
TotalEmployee = 4,
NumberOfServers = 5,
TopBusinessConcern = 6
}
...is a fine declaration.
You do have to cast the result to int like so:
int Question = (int)QuestionType.Role
Otherwise, the type is still QuestionType.
This level of strictness is the C# way.
One alternative is to use a class declaration instead:
public class QuestionType
{
public static int Role = 2,
public static int ProjectFunding = 3,
public static int TotalEmployee = 4,
public static int NumberOfServers = 5,
public static int TopBusinessConcern = 6
}
It's less elegant to declare, but you don't need to cast it in code:
int Question = QuestionType.Role
Alternatively, you may feel more comfortable with Visual Basic, which caters for this type of expectation in many areas.

Maybe I missed it, but has anyone tried a simple generic extension method?
This works great for me. You can avoid the type cast in your API this way but ultimately it results in a change type operation. This is a good case for programming Roslyn to have the compiler make a GetValue<T> method for you.
public static void Main()
{
int test = MyCSharpWrapperMethod(TestEnum.Test1);
Debug.Assert(test == 1);
}
public static int MyCSharpWrapperMethod(TestEnum customFlag)
{
return MyCPlusPlusMethod(customFlag.GetValue<int>());
}
public static int MyCPlusPlusMethod(int customFlag)
{
// Pretend you made a PInvoke or COM+ call to C++ method that require an integer
return customFlag;
}
public enum TestEnum
{
Test1 = 1,
Test2 = 2,
Test3 = 3
}
}
public static class EnumExtensions
{
public static T GetValue<T>(this Enum enumeration)
{
T result = default(T);
try
{
result = (T)Convert.ChangeType(enumeration, typeof(T));
}
catch (Exception ex)
{
Debug.Assert(false);
Debug.WriteLine(ex);
}
return result;
}
}

int number = Question.Role.GetHashCode();
number should have the value 2.

Use:
Question question = Question.Role;
int value = question.GetHashCode();
It will result in value == 2.
This is only true if the enum fits inside an int.

You can do this by implementing an extension method to your defined enum type:
public static class MyExtensions
{
public static int getNumberValue(this Question questionThis)
{
return (int)questionThis;
}
}
This simplifies getting the int value of the current enum value:
Question question = Question.Role;
int value = question.getNumberValue();
or
int value = Question.Role.getNumberValue();

public enum Suit : int
{
Spades = 0,
Hearts = 1,
Clubs = 2,
Diamonds = 3
}
Console.WriteLine((int)(Suit)Enum.Parse(typeof(Suit), "Clubs"));
// From int
Console.WriteLine((Suit)1);
// From a number you can also
Console.WriteLine((Suit)Enum.ToObject(typeof(Suit), 1));
if (typeof(Suit).IsEnumDefined("Spades"))
{
var res = (int)(Suit)Enum.Parse(typeof(Suit), "Spades");
Console.Out.WriteLine("{0}", res);
}

Since enums can be declared with multiple primitive types, a generic extension method to cast any enum type can be useful.
enum Box
{
HEIGHT,
WIDTH,
DEPTH
}
public static void UseEnum()
{
int height = Box.HEIGHT.GetEnumValue<int>();
int width = Box.WIDTH.GetEnumValue<int>();
int depth = Box.DEPTH.GetEnumValue<int>();
}
public static T GetEnumValue<T>(this object e) => (T)e;

The easiest solution I can think of is overloading the Get(int) method like this:
[modifiers] Questions Get(Question q)
{
return Get((int)q);
}
where [modifiers] can generally be same as for the Get(int) method. If you can't edit the Questions class or for some reason don't want to, you can overload the method by writing an extension:
public static class Extensions
{
public static Questions Get(this Questions qs, Question q)
{
return qs.Get((int)q);
}
}

My favourite hack with int or smaller enums:
GetHashCode();
For an enum
public enum Test
{
Min = Int32.MinValue,
One = 1,
Max = Int32.MaxValue,
}
This,
var values = Enum.GetValues(typeof(Test));
foreach (var val in values)
{
Console.WriteLine(val.GetHashCode());
Console.WriteLine(((int)val));
Console.WriteLine(val);
}
outputs
one
1
1
max
2147483647
2147483647
min
-2147483648
-2147483648
Disclaimer:
It doesn't work for enums based on long.

Try this one instead of convert enum to int:
public static class ReturnType
{
public static readonly int Success = 1;
public static readonly int Duplicate = 2;
public static readonly int Error = -1;
}

Following is the extension method
public static string ToEnumString<TEnum>(this int enumValue)
{
var enumString = enumValue.ToString();
if (Enum.IsDefined(typeof(TEnum), enumValue))
{
enumString = ((TEnum) Enum.ToObject(typeof (TEnum), enumValue)).ToString();
}
return enumString;
}

You should have used Type Casting as we can use in any other language.
If your enum is like this-
public enum Question
{
Role = 2,
ProjectFunding = 3,
TotalEmployee = 4,
NumberOfServers = 5,
TopBusinessConcern = 6
}
And you need to cast to an int, then do this-
Question q = Question.Role;
.............
.............
int something = (int) q;
Re-
In C#, there are two types of casting:
Implicit Casting (automatically) - converting a smaller type to a larger type size like-
char -> int -> long -> float -> double
Explicit Casting (manually) - converting a larger type to a smaller size type like-
double -> float -> long -> int -> char
More can be found in here.

The example I would like to suggest "to get an 'int' value from an enum", is
public enum Sample
{
Book = 1,
Pen = 2,
Pencil = 3
}
int answer = (int)Sample.Book;
Now the answer will be 1.

In Visual Basic, it should be:
Public Enum Question
Role = 2
ProjectFunding = 3
TotalEmployee = 4
NumberOfServers = 5
TopBusinessConcern = 6
End Enum
Private value As Integer = CInt(Question.Role)

will give you the a list with all the integer values of the enum :
List enumValues = Enum.GetValues(typeof(EnumClass)).Cast().ToList();

public enum ViewType
{
List = 1,
Table = 2,
};
// You can use the Enum type as a parameter, so any enumeration from any enumerator
// cshtml
// using proyects.Helpers
// #if (Model.ViewType== (int)<variable>.List )

I came up with this extension method that includes current language features. By using dynamic, I don't need to make this a generic method and specify the type which keeps the invocation simpler and consistent:
public static class EnumEx
{
public static dynamic Value(this Enum e)
{
switch (e.GetTypeCode())
{
case TypeCode.Byte:
{
return (byte) (IConvertible) e;
}
case TypeCode.Int16:
{
return (short) (IConvertible) e;
}
case TypeCode.Int32:
{
return (int) (IConvertible) e;
}
case TypeCode.Int64:
{
return (long) (IConvertible) e;
}
case TypeCode.UInt16:
{
return (ushort) (IConvertible) e;
}
case TypeCode.UInt32:
{
return (uint) (IConvertible) e;
}
case TypeCode.UInt64:
{
return (ulong) (IConvertible) e;
}
case TypeCode.SByte:
{
return (sbyte) (IConvertible) e;
}
}
return 0;
}

Related

How to convert the integer associated with a value of an enum to string [duplicate]

I have the following enum:
public enum Urgency {
VeryHigh = 1,
High = 2,
Routine = 4
}
I can fetch an enum "value" as string like this:
((int)Urgency.Routine).ToString() // returns "4"
Note: This is different from:
Urgency.Routine.ToString() // returns "Routine"
(int)Urgency.Routine // returns 4
Is there a way I can create an extension class, or a static utliity class, that would provide some syntactical sugar? :)
You should just be able to use the overloads of Enums ToString method to give it a format string, this will print out the value of the enum as a string.
public static class Program
{
static void Main(string[] args)
{
var val = Urgency.High;
Console.WriteLine(val.ToString("D"));
}
}
public enum Urgency
{
VeryHigh = 1,
High = 2,
Low = 4
}
In order to achieve more "human readable" descriptions for enums (e.g. "Very High" rather than "VeryHigh" in your example) I have decorated enum values with attribute as follows:
public enum MeasurementType
{
Each,
[DisplayText("Lineal Metres")]
LinealMetre,
[DisplayText("Square Metres")]
SquareMetre,
[DisplayText("Cubic Metres")]
CubicMetre,
[DisplayText("Per 1000")]
Per1000,
Other
}
public class DisplayText : Attribute
{
public DisplayText(string Text)
{
this.text = Text;
}
private string text;
public string Text
{
get { return text; }
set { text = value; }
}
}
Then, used an extension method like this:
public static string ToDescription(this Enum en)
{
Type type = en.GetType();
MemberInfo[] memInfo = type.GetMember(en.ToString());
if (memInfo != null && memInfo.Length > 0)
{
object[] attrs = memInfo[0].GetCustomAttributes(
typeof(DisplayText),
false);
if (attrs != null && attrs.Length > 0)
return ((DisplayText)attrs[0]).Text;
}
return en.ToString();
}
You can then just call myEnum.ToDescription() in order to display your enum as more readable text.
If you want to just deal with this enum, use Mark Byer's solution.
For a more general solution:
public static string NumberString(this Enum enVal)
{
return Convert.ToDecimal(enVal).ToString("0");
}
Converting to decimal means you don't need to deal with the 8 different allowed underlying integral types explicitly, as all of them convert losslessly to decimal but not to each other (ulong and long don't convert losslessly between each other but both can handle all the rest). Doing that would probably be faster (esp. if you pick well in your order of comparison), but a lot more verbose for relatively little gain.
Edit:
The above isn't as good as Frankentosh's though, Frankentosh saw through the question to the real problem and solves it very eloquently.
Great stuff ... I have now added an extension method to my project
public static class EnumExtensions
{
public static string NumberString(this Enum enVal)
{
return enVal.ToString("D");
}
}
Now I can get the int value - as a string - by calling Urgency.Routine.NumberString(); Thanks to Frankentosh and Jon :)
a simple approach
((Urgency)4).ToString() // returns "Routine"
You can write an extension method for your specific type:
public static class UrgencyExtension
{
public static string ToIntegerString(this Urgency u)
{
return ((int)u).ToString();
}
}
Use as follows:
Urgency u = Urgency.Routine;
string s = u.ToIntegerString();
How about a little reflection? Should work with all underlying types.
public static class EnumTools
{
public static string ToRawValueString(this Enum e)
{
return e
.GetType()
.GetFields(BindingFlags.Public | BindingFlags.Static)
.First(f => f.Name==e.ToString())
.GetRawConstantValue()
.ToString();
}
}
Then:
Console.WriteLine(Urgency.High.ToRawValueString()); //Writes "2"
If you wanted, you could make the extension method work for all enums:
public static string ToValueString(this Enum enumValue)
{
if (enumValue.GetType().GetEnumUnderlyingType() == typeof(int))
return ((int)(object)enumValue).ToString();
else if (enumValue.GetType().GetEnumUnderlyingType() == typeof(byte))
return ((byte)(object)enumValue).ToString();
...
}

Enum Casting With a Variable

I was hoping to get some help with this. I need a function that can take in a string that is a key for a dictionary and an enum type that is has to be cast to.
The dictionary key will be a number that corresponds to an enum. I need to know how to cast the int into an enum where the enum is variable.
Here is the function as I have it written. This might be more clear than my explanation.
string GetEnum(string keyName, Enum enumType)
{
var defaultOut = "";
Model.Form.TryGetValue(keyName, out defaultOut);
if(defaultOut != ""){
return EnumDescriptionUtility.GetDescription((enumType)Convert.ToInt32(defaultOut));
}
else{
return defaultOut;
}
}
I have used most of this code before. the difference before was that enumType was hard coded to the actual enum. I want to offload all that repetition to a function.
Any help is appreciated.
I'm not 100% sure I understand your Q, though if i do, you want to cast a value to a given enum?
This is an extension method I created recently to parse a value to a given enum. The value in this case is the string name of the enum.
public static T ToEnum<T>(this string val) where T : struct
{
T t;
if (Enum.TryParse(val, out t))
return t;
return default(T);
}
And you would use it like:
public enum MyEnum{
A = 1,
B = 2,
C = 3
}
public enum MyOtherEnum{
D = 1,
E = 2,
F = 3
}
string str = "A";
MyEnum yourEnum = str.ToEnum<MyEnum>();
string str2 = "A";
MyOtherEnum yourOtherEnum = str.ToEnum<MyOtherEnum>();
So if I understand you correctly, you know they type of enum and the integer value. You didn't include any information about your enums so I set up a simple example.
public enum Letter
{
A,
B,
C
}
public enum Number
{
One,
Two,
Three
}
So if you know the type and integer value, you can get an enum like this:
public static Enum GetEnum(Type type, int val)
{
Enum e = null;
if (type == typeof(Letter))
{
e = (Letter)val;
}
else if (type == typeof(Number))
{
e = (Number)val;
}
return e;
}
You will need to inspect its type to use it. Maybe like this:
public static string StringFromEnum(Enum e)
{
string result = null;
if (e.GetType() == typeof(Letter))
{
result = ((Letter)e).ToString();
}
else if (e.GetType() == typeof(Number))
{
result = ((Number)e).ToString();
}
return result;
}
public static void Main(string[] args)
{
int val1 = 2;
Type type1 = typeof(Letter);
int val2 = 0;
Type type2 = typeof(Number);
var result1 = GetEnum(type1, val1);
var result2 = GetEnum(type2, val2);
Console.WriteLine("result1 {0}", StringFromEnum(result1));
Console.WriteLine("result2 {0}", StringFromEnum(result2));
Console.ReadKey();
}
If you just want to get a string representing the name of the enum for a given type and int, you can do the following using Enum.GetName()
public enum Letter
{
A,
B,
C
}
public enum Number
{
One,
Two,
Three
}
public static void Main(string[] args)
{
int val1 = 2;
Type type1 = typeof(Letter);
int val2 = 0;
Type type2 = typeof(Number);
var result1 = Enum.GetName(type1, val1);
var result2 = Enum.GetName(type2, val2);
Console.WriteLine("result1 {0}", result1);
Console.WriteLine("result2 {0}", result2);
Console.ReadKey();
}
If you want the actual enum, I think you have to do something similar to the other answers. It sounds like you want to cast using a Type variable:
Type type1 = typeof(Letter);
var result = (type1) val1; // wrong
And I don't think you can do that. Even with generics you will have to specify type at some point.

Min and Max operations on enum values

Using C#, how can I take the min or max of two enum values?
For example, if I have
enum Permissions
{
None,
Read,
Write,
Full
}
is there a method that lets me do Helper.Max(Permissions.Read, Permissions.Full) and get Permissions.Full, for example?
Enums implement IComparable so you can use:
public static T Min<T>(T a, T b) where T : IComparable
{
return a.CompareTo(b) <= 0 ? a : b;
}
Since enums are convertible to integer types, you can just do:
var permissions1 = Permissions.None;
var permissions2 = Permissions.Full;
var maxPermission = (Permissions) Math.Max((int) permissions1, (int) permissions2);
Note that this could cause issues if your enum is based on an unsigned type, or a type longer than 32 bits (i.e., long or ulong), but in that case you can just change the type you are casting the enums as to match the type declared in your enum.
I.e., for an enum declared as:
enum Permissions : ulong
{
None,
Read,
Write,
Full
}
You would use:
var permissions1 = Permissions.None;
var permissions2 = Permissions.Full;
var maxPermission = (Permissions) Math.Max((ulong) permissions1, (ulong) permissions2);
can be called with 2 or more parameters
public static T GetMaxEnum<T>(params T[] enums) where T : struct, IConvertible
{
if (enums.Length < 2)
{
throw new InvalidEnumArgumentException();
}
return enums.Max();
}
This is what I came up with because I couldn't find anything in .NET that did this.
public static class EnumHelper
{
public static T Min<T>(T a, T b)
{
return (dynamic)a < (dynamic)b ? a : b;
}
public static T Max<T>(T a, T b)
{
return (dynamic)a > (dynamic)b ? a : b;
}
}
While this question is quite old, it shows up at the top for some searches I did, so here we go, with a little more detail than the existing answer:
Permissions p1 = Permissions.Read;
Permissions p2 = Permissions.Write;
var pMax = (Permissions)Math.Max( (int)p1, (int)p2 );
Alternatively in case the enum is long based:
var pMax = (Permissions)Math.Max( (long)p1, (long)p2 );
Why does this work?
enum values can be cast to int (or 'long'), which represents their position
An int can be cast back to an enum
Math.Max() apparently works on int
Sidenotes:
Appearently this also works for the mininum with Math.Min()
IEnumerable.Max() and IEnumerable.Min() can be used if you need the maximum or minimum of more than two enum values (if you don't mind System.Linq).
I think you want something like this:
public enum Enum1
{
A_VALUE,
B_VALUE,
C_VALUE
}
public enum Enum2
{
VALUE_1,
VALUE_2,
VALUE_3
}
class Program
{
static void Main(string[] args)
{
Program p = new Program();
Console.WriteLine(p.EnumMin<Enum1>());
Console.WriteLine(p.EnumMax<Enum2>());
}
T EnumMin<T>()
{
T ret; ;
Array x = Enum.GetValues(typeof(T));
ret = (T) x.GetValue(0);
return ret;
}
T EnumMax<T>()
{
T ret; ;
Array x = Enum.GetValues(typeof(T));
ret = (T)x.GetValue(x.Length-1);
return ret;
}
}
There is a one-stop means for getting the min and max for any enumeration. All it assumes is that the representation type is an int.
public Tuple<int,int> GetMinMaxOfEnumeration<T>()
{
if (!typeof (T).IsEnum)
{
throw new ArgumentException("Type must be an enumeration");
}
var valuesAsInt = Enum.GetValues(typeof (T)).Cast<int>().OrderBy(n => n).ToArray();
return new Tuple<int, int>(valuesAsInt.First(), valuesAsInt.Last());
}

get value from c# enums

I have an enum
public enum ProductionStatus {
Received = 000,
Validated = 010,
PlannedAndConverted = 020,
InProduction = 030,
QAChecked = 040,
Delivered = 070,
RejectedOrCancelled = 100
}
I need to get value by key from this enum, for example when choosing ProductionStatus.Validated it should return 010. How can I do this?
Just to throw another solution in there...
((int)ProductionStatus.Validated).ToString("D3");
var code = (int)ProductionStatus.Validated;
You can also convert an int to an enum value, like this:
var status = (ProductionStatus)10;
bool eq = 010 == 10; they are actually equal
If you would like to use strings , use this method.
static string EnumToString(ProductionStatus val)
{
switch (val)
{
case ProductionStatus.Received:
return "000";
case ProductionStatus.Validated:
return "010";
case ProductionStatus.PlannedAndConverted:
return "020";
default:
return "Unknown value";
}
}
With Formatting:
((int)ProductionStatus.Validated).ToString("000", CultureInfo.InvariantCulture);
That's short and simple, and it returns a string.
You can factor that into an extension method if you like
public static class ProdStatusExtensions {
public static string (this ProductionStatus status) {
return ((int)status).ToString ("000", CultureInfo.InvariantCulture);
}
}
var enumValues = Enum.GetValues(typeof(ProductionStatus)).Cast<object>()
.ToDictionary(enumValue => enumValue.ToString(), enumValue => (int)enumValue);
foreach (var enumValue in enumValues)
{
Console.WriteLine("item: {0}, value: {1}", enumValue.Key, enumValue.Value.ToString("000");
}
You can get all of the values and names from an enum like so.
In general there is an Enum Class that contains an array of methods facilitating the work with enums.
Here, if you want to cast enumerable value to integer or other type, you can write:
int validatedAsInt = (int) ProductionStatus.Validated
validatedAsInt will contain value of ProductionStatus.Validated.
If you want to obtain numbers like "010" you can write:
string validatedAsString = ((int) ProductionStatus.Validated).ToString("000");
Or:
string validatedAsString = ((int) ProductionStatus.Validated).ToString("D3");
validatedAsString will contain "010".
Here is universal helper class that will do reverse action - getting key by value from ANY Enum:
public static class EnumHelpers {
public static T GetEnumObjectByValue<T>(int valueId) {
return (T) Enum.ToObject(typeof (T), valueId);
}
}
And it works like this - given we have this Enum:
public enum ShipmentStatus {
New = 0,
Shipped = 1,
Canceled = 2
}
So, to get Enum object ShipmentStatus.Shipped this will return this object:
var enumObject = EnumHelpers.GetEnumObjectByValue<ShipmentStatus>(1);
So basicaly you can stick any Enum object and get it by value:
var enumObject = EnumHelpers.GetEnumObjectByValue<YOUR_ENUM_TYPE>(VALUE);

Get int value from enum in C#

I have a class called Questions (plural). In this class there is an enum called Question (singular) which looks like this.
public enum Question
{
Role = 2,
ProjectFunding = 3,
TotalEmployee = 4,
NumberOfServers = 5,
TopBusinessConcern = 6
}
In the Questions class I have a get(int foo) function that returns a Questions object for that foo. Is there an easy way to get the integer value off the enum so I can do something like this Questions.Get(Question.Role)?
Just cast the enum, e.g.
int something = (int) Question.Role;
The above will work for the vast majority of enums you see in the wild, as the default underlying type for an enum is int.
However, as cecilphillip points out, enums can have different underlying types.
If an enum is declared as a uint, long, or ulong, it should be cast to the type of the enum; e.g. for
enum StarsInMilkyWay:long {Sun = 1, V645Centauri = 2 .. Wolf424B = 2147483649};
you should use
long something = (long)StarsInMilkyWay.Wolf424B;
Since Enums can be any integral type (byte, int, short, etc.), a more robust way to get the underlying integral value of the enum would be to make use of the GetTypeCode method in conjunction with the Convert class:
enum Sides {
Left, Right, Top, Bottom
}
Sides side = Sides.Bottom;
object val = Convert.ChangeType(side, side.GetTypeCode());
Console.WriteLine(val);
This should work regardless of the underlying integral type.
Declare it as a static class having public constants:
public static class Question
{
public const int Role = 2;
public const int ProjectFunding = 3;
public const int TotalEmployee = 4;
public const int NumberOfServers = 5;
public const int TopBusinessConcern = 6;
}
And then you can reference it as Question.Role, and it always evaluates to an int or whatever you define it as.
On a related note, if you want to get the int value from System.Enum, then given e here:
Enum e = Question.Role;
You can use:
int i = Convert.ToInt32(e);
int i = (int)(object)e;
int i = (int)Enum.Parse(e.GetType(), e.ToString());
int i = (int)Enum.ToObject(e.GetType(), e);
The last two are plain ugly. I prefer the first one.
Question question = Question.Role;
int value = (int) question;
Will result in value == 2.
Example:
public enum EmpNo
{
Raj = 1,
Rahul,
Priyanka
}
And in the code behind to get the enum value:
int setempNo = (int)EmpNo.Raj; // This will give setempNo = 1
or
int setempNo = (int)EmpNo.Rahul; // This will give setempNo = 2
Enums will increment by 1, and you can set the start value. If you don't set the start value it will be assigned as 0 initially.
It's easier than you think - an enum is already an int. It just needs to be reminded:
int y = (int)Question.Role;
Console.WriteLine(y); // Prints 2
I have recently converted away from using enums in my code in favour of instead using classes with protected constructors and predefined static instances (thanks to Roelof - C# Ensure Valid Enum Values - Futureproof Method).
In light of that, below's how I'd now approach this issue (including implicit conversion to/from int).
public class Question
{
// Attributes
protected int index;
protected string name;
// Go with a dictionary to enforce unique index
//protected static readonly ICollection<Question> values = new Collection<Question>();
protected static readonly IDictionary<int,Question> values = new Dictionary<int,Question>();
// Define the "enum" values
public static readonly Question Role = new Question(2,"Role");
public static readonly Question ProjectFunding = new Question(3, "Project Funding");
public static readonly Question TotalEmployee = new Question(4, "Total Employee");
public static readonly Question NumberOfServers = new Question(5, "Number of Servers");
public static readonly Question TopBusinessConcern = new Question(6, "Top Business Concern");
// Constructors
protected Question(int index, string name)
{
this.index = index;
this.name = name;
values.Add(index, this);
}
// Easy int conversion
public static implicit operator int(Question question) =>
question.index; //nb: if question is null this will return a null pointer exception
public static implicit operator Question(int index) =>
values.TryGetValue(index, out var question) ? question : null;
// Easy string conversion (also update ToString for the same effect)
public override string ToString() =>
this.name;
public static implicit operator string(Question question) =>
question?.ToString();
public static implicit operator Question(string name) =>
name == null ? null : values.Values.FirstOrDefault(item => name.Equals(item.name, StringComparison.CurrentCultureIgnoreCase));
// If you specifically want a Get(int x) function (though not required given the implicit converstion)
public Question Get(int foo) =>
foo; //(implicit conversion will take care of the conversion for you)
}
The advantage of this approach is you get everything you would have from the enum, but your code's now much more flexible, so should you need to perform different actions based on the value of Question, you can put logic into Question itself (i.e. in the preferred OO fashion) as opposed to putting lots of case statements throughout your code to tackle each scenario.
NB: Answer updated 2018-04-27 to make use of C# 6 features; i.e. declaration expressions and lambda expression body definitions. See revision history for original code. This has the benefit of making the definition a little less verbose; which had been one of the main complaints about this answer's approach.
If you want to get an integer for the enum value that is stored in a variable, for which the type would be Question, to use for example in a method, you can simply do this I wrote in this example:
enum Talen
{
Engels = 1, Italiaans = 2, Portugees = 3, Nederlands = 4, Duits = 5, Dens = 6
}
Talen Geselecteerd;
public void Form1()
{
InitializeComponent()
Geselecteerd = Talen.Nederlands;
}
// You can use the Enum type as a parameter, so any enumeration from any enumerator can be used as parameter
void VeranderenTitel(Enum e)
{
this.Text = Convert.ToInt32(e).ToString();
}
This will change the window title to 4, because the variable Geselecteerd is Talen.Nederlands. If I change it to Talen.Portugees and call the method again, the text will change to 3.
One more way to do it:
Console.WriteLine("Name: {0}, Value: {0:D}", Question.Role);
It will result in:
Name: Role, Value: 2
To ensure an enum value exists and then parse it, you can also do the following.
// Fake Day of Week
string strDOWFake = "SuperDay";
// Real Day of Week
string strDOWReal = "Friday";
// Will hold which ever is the real DOW.
DayOfWeek enmDOW;
// See if fake DOW is defined in the DayOfWeek enumeration.
if (Enum.IsDefined(typeof(DayOfWeek), strDOWFake))
{
// This will never be reached since "SuperDay"
// doesn't exist in the DayOfWeek enumeration.
enmDOW = (DayOfWeek)Enum.Parse(typeof(DayOfWeek), strDOWFake);
}
// See if real DOW is defined in the DayOfWeek enumeration.
else if (Enum.IsDefined(typeof(DayOfWeek), strDOWReal))
{
// This will parse the string into it's corresponding DOW enum object.
enmDOW = (DayOfWeek)Enum.Parse(typeof(DayOfWeek), strDOWReal);
}
// Can now use the DOW enum object.
Console.Write("Today is " + enmDOW.ToString() + ".");
Use an extension method instead:
public static class ExtensionMethods
{
public static int IntValue(this Enum argEnum)
{
return Convert.ToInt32(argEnum);
}
}
And the usage is slightly prettier:
var intValue = Question.Role.IntValue();
public enum QuestionType
{
Role = 2,
ProjectFunding = 3,
TotalEmployee = 4,
NumberOfServers = 5,
TopBusinessConcern = 6
}
...is a fine declaration.
You do have to cast the result to int like so:
int Question = (int)QuestionType.Role
Otherwise, the type is still QuestionType.
This level of strictness is the C# way.
One alternative is to use a class declaration instead:
public class QuestionType
{
public static int Role = 2,
public static int ProjectFunding = 3,
public static int TotalEmployee = 4,
public static int NumberOfServers = 5,
public static int TopBusinessConcern = 6
}
It's less elegant to declare, but you don't need to cast it in code:
int Question = QuestionType.Role
Alternatively, you may feel more comfortable with Visual Basic, which caters for this type of expectation in many areas.
Maybe I missed it, but has anyone tried a simple generic extension method?
This works great for me. You can avoid the type cast in your API this way but ultimately it results in a change type operation. This is a good case for programming Roslyn to have the compiler make a GetValue<T> method for you.
public static void Main()
{
int test = MyCSharpWrapperMethod(TestEnum.Test1);
Debug.Assert(test == 1);
}
public static int MyCSharpWrapperMethod(TestEnum customFlag)
{
return MyCPlusPlusMethod(customFlag.GetValue<int>());
}
public static int MyCPlusPlusMethod(int customFlag)
{
// Pretend you made a PInvoke or COM+ call to C++ method that require an integer
return customFlag;
}
public enum TestEnum
{
Test1 = 1,
Test2 = 2,
Test3 = 3
}
}
public static class EnumExtensions
{
public static T GetValue<T>(this Enum enumeration)
{
T result = default(T);
try
{
result = (T)Convert.ChangeType(enumeration, typeof(T));
}
catch (Exception ex)
{
Debug.Assert(false);
Debug.WriteLine(ex);
}
return result;
}
}
int number = Question.Role.GetHashCode();
number should have the value 2.
Use:
Question question = Question.Role;
int value = question.GetHashCode();
It will result in value == 2.
This is only true if the enum fits inside an int.
You can do this by implementing an extension method to your defined enum type:
public static class MyExtensions
{
public static int getNumberValue(this Question questionThis)
{
return (int)questionThis;
}
}
This simplifies getting the int value of the current enum value:
Question question = Question.Role;
int value = question.getNumberValue();
or
int value = Question.Role.getNumberValue();
public enum Suit : int
{
Spades = 0,
Hearts = 1,
Clubs = 2,
Diamonds = 3
}
Console.WriteLine((int)(Suit)Enum.Parse(typeof(Suit), "Clubs"));
// From int
Console.WriteLine((Suit)1);
// From a number you can also
Console.WriteLine((Suit)Enum.ToObject(typeof(Suit), 1));
if (typeof(Suit).IsEnumDefined("Spades"))
{
var res = (int)(Suit)Enum.Parse(typeof(Suit), "Spades");
Console.Out.WriteLine("{0}", res);
}
Since enums can be declared with multiple primitive types, a generic extension method to cast any enum type can be useful.
enum Box
{
HEIGHT,
WIDTH,
DEPTH
}
public static void UseEnum()
{
int height = Box.HEIGHT.GetEnumValue<int>();
int width = Box.WIDTH.GetEnumValue<int>();
int depth = Box.DEPTH.GetEnumValue<int>();
}
public static T GetEnumValue<T>(this object e) => (T)e;
The easiest solution I can think of is overloading the Get(int) method like this:
[modifiers] Questions Get(Question q)
{
return Get((int)q);
}
where [modifiers] can generally be same as for the Get(int) method. If you can't edit the Questions class or for some reason don't want to, you can overload the method by writing an extension:
public static class Extensions
{
public static Questions Get(this Questions qs, Question q)
{
return qs.Get((int)q);
}
}
My favourite hack with int or smaller enums:
GetHashCode();
For an enum
public enum Test
{
Min = Int32.MinValue,
One = 1,
Max = Int32.MaxValue,
}
This,
var values = Enum.GetValues(typeof(Test));
foreach (var val in values)
{
Console.WriteLine(val.GetHashCode());
Console.WriteLine(((int)val));
Console.WriteLine(val);
}
outputs
one
1
1
max
2147483647
2147483647
min
-2147483648
-2147483648
Disclaimer:
It doesn't work for enums based on long.
Try this one instead of convert enum to int:
public static class ReturnType
{
public static readonly int Success = 1;
public static readonly int Duplicate = 2;
public static readonly int Error = -1;
}
Following is the extension method
public static string ToEnumString<TEnum>(this int enumValue)
{
var enumString = enumValue.ToString();
if (Enum.IsDefined(typeof(TEnum), enumValue))
{
enumString = ((TEnum) Enum.ToObject(typeof (TEnum), enumValue)).ToString();
}
return enumString;
}
You should have used Type Casting as we can use in any other language.
If your enum is like this-
public enum Question
{
Role = 2,
ProjectFunding = 3,
TotalEmployee = 4,
NumberOfServers = 5,
TopBusinessConcern = 6
}
And you need to cast to an int, then do this-
Question q = Question.Role;
.............
.............
int something = (int) q;
Re-
In C#, there are two types of casting:
Implicit Casting (automatically) - converting a smaller type to a larger type size like-
char -> int -> long -> float -> double
Explicit Casting (manually) - converting a larger type to a smaller size type like-
double -> float -> long -> int -> char
More can be found in here.
The example I would like to suggest "to get an 'int' value from an enum", is
public enum Sample
{
Book = 1,
Pen = 2,
Pencil = 3
}
int answer = (int)Sample.Book;
Now the answer will be 1.
In Visual Basic, it should be:
Public Enum Question
Role = 2
ProjectFunding = 3
TotalEmployee = 4
NumberOfServers = 5
TopBusinessConcern = 6
End Enum
Private value As Integer = CInt(Question.Role)
will give you the a list with all the integer values of the enum :
List enumValues = Enum.GetValues(typeof(EnumClass)).Cast().ToList();
public enum ViewType
{
List = 1,
Table = 2,
};
// You can use the Enum type as a parameter, so any enumeration from any enumerator
// cshtml
// using proyects.Helpers
// #if (Model.ViewType== (int)<variable>.List )
I came up with this extension method that includes current language features. By using dynamic, I don't need to make this a generic method and specify the type which keeps the invocation simpler and consistent:
public static class EnumEx
{
public static dynamic Value(this Enum e)
{
switch (e.GetTypeCode())
{
case TypeCode.Byte:
{
return (byte) (IConvertible) e;
}
case TypeCode.Int16:
{
return (short) (IConvertible) e;
}
case TypeCode.Int32:
{
return (int) (IConvertible) e;
}
case TypeCode.Int64:
{
return (long) (IConvertible) e;
}
case TypeCode.UInt16:
{
return (ushort) (IConvertible) e;
}
case TypeCode.UInt32:
{
return (uint) (IConvertible) e;
}
case TypeCode.UInt64:
{
return (ulong) (IConvertible) e;
}
case TypeCode.SByte:
{
return (sbyte) (IConvertible) e;
}
}
return 0;
}

Categories