Skip to main content
added 286 characters in body
Source Link

Be sure when you ever have a code that it has the chain of if else, there should be an improvement. (even many programmers say you should not use if statement in your code as possible and some others say don't use else statement at all!)

Be sure when you ever have a code that it has the chain of if else, there should be an improvement.

Be sure when you ever have a code that it has the chain of if else there should be an improvement. (even many programmers say you should not use if statement in your code as possible and some others say don't use else statement at all!)

improve answer
Source Link
public class ActionRegistry: Dictionary<int, Func<int, int, int>>
{
    public ActionRegistry()
    {
       //it's better to register your actions outside the class
       // and don't use enum inside your class
       // but for simplicity i did that inside the class 

       this.Add((int)ActionType.Add, (x, y) => x + y);
       this.Add((int)ActionType.Subtract, (x, y) => x - y);
    }

}
public class ActionRegistry: Dictionary<int, Func<int, int, int>>
{
    public ActionRegistry()
    {
       this.Add((int)ActionType.Add, (x, y) => x + y);
       this.Add((int)ActionType.Subtract, (x, y) => x - y);
    }

}
public class ActionRegistry: Dictionary<int, Func<int, int, int>>
{
    public ActionRegistry()
    {
       //it's better to register your actions outside the class
       // and don't use enum inside your class
       // but for simplicity i did that inside the class 

       this.Add((int)ActionType.Add, (x, y) => x + y);
       this.Add((int)ActionType.Subtract, (x, y) => x - y);
    }

}
improve answer
Source Link

UPDATE Here my update using enums:

First Declare an Enum :

enum ActionType 
{ 
 Add, 
 Subtract 
}

Then use that enum in your ActionRegistry class:

public class ActionRegistry: Dictionary<int, Func<int, int, int>>
{
    public ActionRegistry()
    {
       this.Add((int)ActionType.Add, (x, y) => x + y);
       this.Add((int)ActionType.Subtract, (x, y) => x - y);
    }

}

then you should change your calculate method like this :

 private int Calculate(int actionTypeCode)
        {
        var actionRegistry= new ActionRegistry();
        var a = 1;
        var b = 2;
        //var actionResult= actionRegistry[this should come from your drop down].Invoke(a, b);
        var actionResult= actionRegistry[actionTypeCode].Invoke(a, b);

        return actionResult;
    }

Note that you should bind your dropdown list with your enum keys as value. I prefer to use an integer as my key because I can add more item later without changing my enum but it is not necessary.

UPDATE Here my update using enums:

First Declare an Enum :

enum ActionType 
{ 
 Add, 
 Subtract 
}

Then use that enum in your ActionRegistry class:

public class ActionRegistry: Dictionary<int, Func<int, int, int>>
{
    public ActionRegistry()
    {
       this.Add((int)ActionType.Add, (x, y) => x + y);
       this.Add((int)ActionType.Subtract, (x, y) => x - y);
    }

}

then you should change your calculate method like this :

 private int Calculate(int actionTypeCode)
        {
        var actionRegistry= new ActionRegistry();
        var a = 1;
        var b = 2;
        //var actionResult= actionRegistry[this should come from your drop down].Invoke(a, b);
        var actionResult= actionRegistry[actionTypeCode].Invoke(a, b);

        return actionResult;
    }

Note that you should bind your dropdown list with your enum keys as value. I prefer to use an integer as my key because I can add more item later without changing my enum but it is not necessary.

added 8 characters in body
Source Link
Loading
deleted 24 characters in body
Source Link
Mast
  • 13.5k
  • 12
  • 54
  • 119
Loading
Source Link
Loading