Private delegates in C# -


i'm working class contains several variants of private method. currently, i'm using enum select appropriate 1 within exposed method so.

public class myclass {     public enum myenum { type1, type2, type3, type4 };      private myenum _type;       public myclass(myenum type)     {         type = type;     }       public myenum type     {         { return _type; }         set { _type = value; }     }       public int function(int x, int y)     {         switch(_type)         {             case myenum.type1:                 return function1(x,y);             case myenum.type2:                 return function2(x,y);             case myenum.type3:                 return function3(x, y);             case myenum.type4:                 return function4(x, y);         }     }       private int function1(int x, int y)     {         // function variant 1     }       private int function2(int x, int y)     {         // function variant 2     }       private int function3(int x, int y)     {         // function variant 3     }       private int function4(int x, int y)     {         // function variant 4     }  } 

this works fine i'm wondering if i'd better off going private delegate updated whenever enum changes. since, in case, public method called more enum setter.

public class myclass {     public enum myenum { type1, type2, type3, type4 };      private func<int, int, int> _function;     private myenum _type;       public myclass(myenum type)     {         type = type;     }       public myenum type     {         { return _type; }         set         {             _type = value;             ontypechange();         }     }       private void ontypechange()     {         switch (_type)         {             case myenum.type1:                 _function = function1;                 return;             case myenum.type2:                 _function = function2;                 return;             case myenum.type3:                 _function = function3;                 return;             case myenum.type4:                 _function = function4;                 return;         }     }       public int function(int x, int y)     {         return _function(x, y);     }       private int function1(int x, int y)     {         // function variant 1     }       private int function2(int x, int y)     {         // function variant 2     }       private int function3(int x, int y)     {         // function variant 3     }       private int function4(int x, int y)     {         // function variant 4     }  } 

i suppose i'm looking conventional wisdom on subject. how sort of thing typically handled out in wild?

your second option technically better, because don't have go through switch every time public method called.

the first bit more readable.

in reality though, switching behavior on enum decent sized red flag. subclass myclass , use polymorphism desired behavior. consider doing in case. "in wild", that's approach use.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -