Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Better solution than using too many switch cases

Tags:

c#

selenium

I have this switch case to identify value from Excel and check to which function to call based on value pass in DemoName but I need a better solution than this:

public void SelectDemoAccount(string dataKeyname, string sheetName)
{
    var Demo = new Demoing(DriverHelper.GetDriver);
    var ud = ExcelDataHelper.GetTestData(dataKeyname, sheetName);
    String DemoName = ud.DemoName;
    switch (DemoName)
    {
        case "Abc":
            Demo.ClickAbc();
            break;
        case "Def":
            Demo.ClickDef();
            break;
        case "Ghi":
            Demo.ClickGhi();
            break;
        case "Dsa":
            Demo.ClickDsa();
            break;
        case "Qwe":
            Demo.ClickQwe();
            break;
        case "Zxc":
            Demo.ClickZxc();
            break;
        case "Hjk":
            Demo.ClickHjk();
            break;
        case "Fgh":
            Demo.ClickFgh();
            break;
        case "Cxz":
            Demo.ClickCxz();
            break;
        case "Nmq":
            Demo.ClickNmq();
            break;
        case "Atb":
            Demo.ClickAtb();
            break;
        case "Smt":
            Demo.ClickSmt();
            break;
        case "Mdt":
            Demo.ClickMdt();
            break;
        case "Ldt":
            Demo.ClickLdt();
            break;
        case "Cdt":
            Demo.ClickCdt();
            break;
        case "Hdt":
            Demo.ClickHdt();
            break;
    }    
}
like image 374
harsh suthar Avatar asked Apr 14 '26 00:04

harsh suthar


2 Answers

You can create a Dictionary in which the Key is your string used in the current switch code and the Value is the method to execute from the demo variable.

var Demo = new Demoing(DriverHelper.GetDriver);
Dictionary<string, Action> demoMethods = new Dictionary<string, Action>();
demoMethods.Add("Abc", Demo.ClickAbc);
... add the other keyvalue pairs here

Now when you need to call the method just use

var ud = ExcelDataHelper.GetTestData(dataKeyname, sheetName);
String DemoName = ud.DemoName;
demoMethods[DemoName].Invoke();

This assumes that you have always the same instance of Demo to use, otherwise, if the code called is not static but requires to be executed on an instance of a Demoing class you need to have a set of static methods in your Demoing class. Each static method receives an instance of Demoing class to work with the underlying method.

public class Demoing
{
     public static void ClickAbc(Demoing current)
     {
        current.DoSomething();
        .... whatever ....
     }
}

So now you can prepare your dictionary using a different Action value,

Dictionary<string, Action<Demo>> demoMethods = new Dictionary<string, Action<Demo>>();
demoMethods.Add("Abc", Demoing.ClickAbc);

And when you need to call the method with a specific instance you write

var Demo = new Demoing(DriverHelper.GetDriver);
var ud = ExcelDataHelper.GetTestData(dataKeyname, sheetName);
String DemoName = ud.DemoName;
demoMethods[DemoName].Invoke(Demo);

Finally there is another way to employ the dictionary without using static methods and keep everything encapsulated inside the Demoing class. This idea comes from an answer now deleted by Tim Schmelter.
Here the dictionary is internal to the Demoing class and a public method is given to access the dictionary actions as with:

public class Demoing
{
    private Dictionary<string, Action> nameActions;
      
    private Demoing()
    {
        InitNameActions();
    }

    private void InitNameActions()
    {
        nameActions = new Dictionary<string, System.Action>(StringComparer.OrdinalIgnoreCase)
        {
            {"Abc", ClickAbc},
            // ...
        }
    }

    public Action GetAction(string name){
        return nameActions.TryGetValue(name, out Action action) ? action : null;
    }
}

and the call is the following

var demo = new Demoing(DriverHelper.GetDriver);
String demoName = ud.DemoName;
Action action = demo.GetAction(demoName);
action?.Invoke();

This way is better from an encapsulation stand point. But keep in mind that now each object of type Demoing has its Dictionary and, if you have a lot of Demoing objects around this could be not optimal for a memory footprint.

like image 146
Steve Avatar answered Apr 16 '26 14:04

Steve


Another way is using reflection, it's slower but it works, and it requires less lines of code:

 var Demo = new Demoing(DriverHelper.GetDriver);
 var ud = ExcelDataHelper.GetTestData(dataKeyname, sheetName);
 var mi = typeof(Demoing).GetMethod("Click" + ud.DemoName);
 mi.Invoke(Demo);
    
like image 28
Jesús López Avatar answered Apr 16 '26 14:04

Jesús López



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!