Reputation: 745
I have an enum type defined as follows:
public enum OperationTypeEnum : byte
{
/// <remarks/>
@__NONE = 0,
/// <remarks/>
Sale = 1,
/// <remarks/>
Auth = 2
}
In my code, I cast an integer like so:
var operationType = (OperationTypeEnum) anotherIntVariable;
When anotherIntVariable is something undefined (e.g. 5), I am hoping to get 0 or __NONE back (since 5 is not defined as one of the valid enum values, but I receive 5 instead.
What do I need to change to make undefined enum values to be 0?
Thanks!
Upvotes: 0
Views: 2931
Reputation: 728
#If your enum class is defined as below.
public enum CategoryEnum : int
{
Undefined = 0,
IT= 1,
HR= 2,
Sales= 3,
Finance=4
}
Now if you want to find Undefined enum if value doesnot match 1,2,3,4 then using the following Enum.IsDefine() function
Enum.IsDefined(typeof(CategoryEnum), request.CategoryId)
#returns true if id matches any enum values
#else returns false
Now for parsing the enum values as string
public enum Mode
{
UNDEFINED = 0,
APP = 1,
TEXT = 2,
}
var value = Enum.TryParse(request.Mode, true, out Mode mode);
#returns mode enum
#Now to get Corresponding value for id or vice-vera, have an extension method as below:
public static T AsEnum<T>(this string input) where T : struct, IConvertible
{
if (!typeof(T).IsEnum)
{
throw new ArgumentException("T must be an enumerated type");
}
Enum.TryParse(input, ignoreCase: true, out T result);
return (T)result;
}
var status = searchText.AsEnum<StatusEnum>();
Upvotes: 0
Reputation: 7837
The answer was given by @plast1k.
Here is a generic extension for your problem
public static class OperationTypeEnumExtensions
{
public static T ToEnum<T>(this byte val) where T : struct
{
if(Enum.IsDefined(typeof(T), val))
return (T) Enum.Parse(typeof(T), val.ToString());
return default(T);
}
}
usage
value.ToEnum<OperationTypeEnum>()
Upvotes: 2
Reputation: 30464
The safest method is to create an extension method for your enum class that will compare the enum values and if none of them initialize to @__NONE
If you are certain there are only a limited amount of enum values:
public static class OperationTypeEnumExtensions
{
public static OperationTypeEnum ToOperationTypeEnum(this int val)
{
switch (val)
{
case (int) OperatinTypeEnum.Sale:
return OperationTypeEnum.Sale;
cast (int) OperationTypeEnum.Auth:
return OperationTypeenum.Auth;
default:
return OperationTypeEnum.@_none;
}
}
}
Usage:
int myInt = GetMyIntValue();
OperationTypeEnum myEnum = myInt.ToOperationTypeEnum();
Upvotes: 0
Reputation: 843
C# enums are effectively integers and there are no compile or run time checks that you are using a "valid" value from your defined enum set. See this answer for more information https://stackoverflow.com/a/6413841/1724034
Upvotes: 1
Reputation: 28573
If you get a numerical value and need to only get actual enum values, you can use Enum.TryParse
The example displays the following output:
Converted '0' to None.
Converted '2' to Green.
8 is not an underlying value of the Colors enumeration.
blue is not a member of the Colors enumeration.
Converted 'Blue' to Blue.
Yellow is not a member of the Colors enumeration.
using System;
[Flags] enum Colors { None=0, Red = 1, Green = 2, Blue = 4 };
public class Example
{
public static void Main()
{
string[] colorStrings = { "0", "2", "8", "blue", "Blue", "Yellow", "Red, Green" };
foreach (string colorString in colorStrings)
{
Colors colorValue;
if (Enum.TryParse(colorString, out colorValue))
if (Enum.IsDefined(typeof(Colors), colorValue) | colorValue.ToString().Contains(","))
Console.WriteLine("Converted '{0}' to {1}.", colorString, colorValue.ToString());
else
Console.WriteLine("{0} is not an underlying value of the Colors enumeration.", colorString);
else
Console.WriteLine("{0} is not a member of the Colors enumeration.", colorString);
}
}
}
Upvotes: 0