Run Code
|
API
|
Code Wall
|
Users
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Blog
Fun with Enums
//Rextester.Program.Main is the entry point for your code. Don't change it. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5 /* Fun with Enums -------------- By: vsj.1987@live.in Aug 12, 2017 Enums are little structures which can be used as ErrorCodes in systems. They are often used as "named" variables for certain constant values. This example shows how we can use Enums as Error Codes in any C# software. This example shows: i. different extension methods ii. adding Description attribute to Enum iii. get Enum based on their values and Descriptions using Reflection iv. making an IEnumerable<> out of an Enum v. Misc conversions */ using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; using System.Reflection; namespace Rextester { public class Program { public static void Main(string[] args) { //Get Enum string and get Enum value Console.WriteLine(ErrVal.CP_NOTOPEN.ToString()+" " +(int)ErrVal.CP_DATEOVERLAP); //we can have Enums which are int and starts with Zero Console.WriteLine((int)ErrVal.CP_DATEOVERLAP==-031611);//true Console.WriteLine((int)ErrVal.CP_DATEOVERLAP==-31611);//true Console.WriteLine((int)ErrVal.CP_NOTOPEN==-031611);//false //Reverse map Description with Enum. i.e., Get an enum based on Text description. Useful when having "description" in dropdown and then getting the Enum back ErrVal v1 = ErrCode.FromEnumStringValue<ErrVal>("Current order dates are overlapping"); Console.WriteLine("Get Enum from Description: "+v1+" : "+(int)v1); //Get Enum's description. When you pass ErrCode to UI, use this to show the message! Console.WriteLine("Get Description of Enum: "+ErrVal.CP_NOTOPEN.StringValue()); //make an IEnumerable of the Enum and print it IEnumerable<string> errCodes = ErrCode.EnumDropDown<ErrVal>(); //Console.WriteLine("\n"+errCodes.Aggregate((i, j) => i + "\n, " + j)); //aggregate causes O(n^2) Console.WriteLine("\n"+String.Join(",\n", errCodes)); //Print Enum with Description Console.WriteLine("\n"); foreach(string str in errCodes) { ErrVal enm = str.ToEnum<ErrVal>(); Console.WriteLine(enm+": "+enm.StringValue());//best place to create a dropdown with [name{Description} - value{Enum}] } } } public enum ErrVal { [Description("Not set")] NotSet = 0, [Description("Unknown error")] Unknown = 1, [Description("Current order dates are overlapping")] CP_DATEOVERLAP = -031611, [Description("The order is not in an open state. It is either Delivered or Cancelled.")] CP_NOTOPEN = -031622 } internal class DescriptionAttribute : Attribute { private string v; public DescriptionAttribute(string v) { this.v = v; } public string Value { get { return v; } } } public static class ErrCode { //Read //https://stackoverflow.com/a/13869054 public static T FromEnumStringValue<T>(this string description) where T : struct { return (T)typeof(T) .GetFields() .First(f => f.GetCustomAttributes<DescriptionAttribute>() .Any(a => a.Value.Equals(description, StringComparison.OrdinalIgnoreCase)) ) .GetValue(null); } public static string StringValue(this Enum enumItem) { return enumItem .GetType() .GetField(enumItem.ToString()) .GetCustomAttributes<DescriptionAttribute>() .Select(a => a.Value) .FirstOrDefault() ?? enumItem.ToString(); } public static IEnumerable<string> EnumDropDown<T>() { IEnumerable<T> values = Enum.GetValues(typeof(T)).Cast<T>(); IEnumerable<string> items = from value in values orderby value descending select value.ToString(); return items; } public static T ToEnum<T>(this string enumString) { return (T) Enum.Parse(typeof (T), enumString); } } }
run
|
edit
|
history
|
help
0
Great code snippet on
converting int to enum in C#
. Thank you for sharing.
by programmerdude2341@gmail.com, 1 years ago
Please
log in
to post a comment.
Prime Fibonacci
compiled prog
How to find the first index of a sub-string in a given string?
CommandForce3
helloworld_java
sss
How to count the occurrence of each character in a string?
FixConsectiveDuplicates
Boo socks
mine
stackse - search stackoverflow differently
Great code snippet on converting int to enum in C#. Thank you for sharing.
by programmerdude2341@gmail.com, 1 years agoPlease log in to post a comment.