Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in H:\root\home\sklibraries-001\www\rajmhr\wp-content\plugins\wp-markdown\markdownify\markdownify.php on line 299
Enum with attribute – RajTech

Enum with attribute

We are familiar with “enum” and its uses. Most of us use enum to make a strong data type. However, “enum” can be used for various purposes; attribute can be applied to enum to store multiple and different data type.

public  enum Tax
   {
       [TaxAttr(8, 6)]
       Iowa,
       [TaxAttr(5, 6)]
       Texas,
       [TaxAttr(7, 7)]
       Verginia

This is simple Enum declaration. Here, “Tax” is clearly enum, which has‘Iowa’,’Texas’,’Verginia’ as a member. “TaxAttr” is the attribute class, use to hold multiple value for enum member. Which is shown below:

public class TaxAttr : Attribute
   {
       double _local;
       double _state;
       public TaxAttr(double local, double state)
       {
           _local = local;
           _state = state;
       }
       public double Local
       {
           get
           {
               return _local;
           }
       }
       public double Sate
       {
           get
           {
               return _state;
           }
       }

TaxAttr class takes local and state taxes as constructor parameter which is derived from Attribute class. “TaxAttr” attribute class is attributed to enum members. This way, each enum member holds different values. To retrieve those values we use reflection and extension method to ease of access.

public static class Taxs
 {
     public static TaxAttr GetAttr(this Tax p)
     {
         return (TaxAttr)Attribute.GetCustomAttribute(ForValue(p), typeof(TaxAttr));
     }
     private static MemberInfo ForValue(Tax p)
     {
         return typeof(Tax).GetField(Enum.GetName(typeof(Tax), p));
     }
 }

To put it all together, i have a sample program which calculate income of a person living in a particular state.

Enum “Tax” holds tax value for particular state.

public class Calulate
   {
       double _salary;
       Tax _tState;
       public Calulate(double salary, Tax state)
       {
           _tState=state;
           _salary = salary;
       }
       public double CalculateSalary()
       {
           String data = Tax.Iowa.ToString();
           double tSalary = 0.00;
           TaxAttr attr = _tState.GetAttr();
           switch (_tState)
           {
               case Tax.Iowa:
                   {
                       tSalary = _salary * (1 - attr.Local - attr.Sate);
                       break;
                   }
               case Tax.Texas:
                   {
                       tSalary = _salary * (1 - attr.Local - attr.Sate);
                       break;
                   }
               case Tax.Verginia:
                   {
                       tSalary = _salary * (1 - attr.Local - attr.Sate);
                       break;
                   }
           }
           return tSalary;
       }
   }
   public  enum Tax
   {
       [TaxAttr(8, 6)]
       Iowa,
       [TaxAttr(5, 6)]
       Texas,
       [TaxAttr(7, 7)]
       Verginia
   }
   public static class Taxs
   {
       public static TaxAttr GetAttr(this Tax p)
       {
           return (TaxAttr)Attribute.GetCustomAttribute(ForValue(p), typeof(TaxAttr));
       }
       private static MemberInfo ForValue(Tax p)
       {
           return typeof(Tax).GetField(Enum.GetName(typeof(Tax), p));
       }
   }
   public class TaxAttr : Attribute
   {
       double _local;
       double _state;
       public TaxAttr(double local, double state)
       {
           _local = local;
           _state = state;
       }
       public double Local
       {
           get
           {
               return _local;
           }
       }
       public double Sate
       {
           get
           {
               return _state;
           }
       }
   }
 class Program
   {
       static void Main(string[] args)
       {
           Tax tState = ParseEnum<Tax>("Iowa");
           Calulate calculate = new Calulate(30000, tState);
           calculate.CalculateSalary();
       }
       public static T ParseEnum<T>(String val)
       {
           return (T)Enum.Parse(typeof(T), val, true);
       }
   }

So, the summary is, you can use enum  in .net with attribute in many different ways.

Keep learning.!

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *